+ All Categories
Home > Documents > PHP and XML

PHP and XML

Date post: 22-Feb-2016
Category:
Upload: caron
View: 29 times
Download: 0 times
Share this document with a friend
Description:
PHP and XML. TP2653 Advance Web Programming. PHP and XML. PHP5 – XML-based extensions, library and functionalities (current XAMPP PHP version is 5.3.10) Libraries – libxml2 and libxslt Supports XML, Namespaces, Schemas, Relax NG, XPath , XInclude etc. Core XML Extensions. - PowerPoint PPT Presentation
22
PHP and XML TP2653 Advance Web Programming
Transcript
Page 1: PHP and XML

PHP and XML

TP2653 Advance Web Programming

Page 2: PHP and XML

PHP and XML

• PHP5 – XML-based extensions, library and functionalities (current XAMPP PHP version is 5.3.10)

• Libraries – libxml2 and libxslt– Supports XML, Namespaces, Schemas, Relax NG,

XPath, XInclude etc.

Page 3: PHP and XML

Core XML Extensions

• Core XML Extensions:1. Parsers - tree-based (DOM, SimpleXML) and

streaming (SAX, XMLReader) parsers 2. XSL Extension3. Data Exchange & Web Services4. libxml extension

Page 4: PHP and XML

Parsing XML using PHP (1)

• PHP5 supports XML parsing better than PHP4• XML parsers in PHP5:

•Easy object-oriented tree-based XML parsing•Modeled after Perl’s XML:Simple

simplexml

•Object-oriented tree-based XML navigation, creation and modification

DOM

•SAX2 event-based parsing•Backward compatible with PHP4 parser

xml

•Stream-oriented, forward only XML parsing•Based on MS System.XML. XMLReader

xmlreader

Page 5: PHP and XML

Tree-based Parsers (1.1)

• Tree-based Parsers:– Allow to construct/load XML documents to

navigate/modify them– XML documents is created and loaded into

memory as a tree – can be slow– DOM extensions and SimpleXML

Page 6: PHP and XML

SimpleXML (1.1.a)

• SimpleXML – simple and lightweight tool to manipulate XML documents– Easy to learn API– Allow viewing XML as tree of objects– Accessing child elements using elements name as

property of object

Page 7: PHP and XML

DOM Extension (1.1.b)

• DOM extension – replacement for domxml– Large and complex API– Allow access for all node types, create and modify

complex documents– Advance navigation and functionality

Page 8: PHP and XML

Streaming Parsers (1.2)

• Stream-based parsers:– Doesn’t load entire document into memory– Only allow small pieces of document to be

processed– Push parser via xml extension and pull parser via

XMLReader– Don’t allow editing and almost no navigational

capabilities

Page 9: PHP and XML

xml Extension (1.2.a)

• xml extension:– SAX-based tool, offers event-based parsing– Handlers – function assigned to events– Push parser - not in control of data sent to the

functions– Parsing starts -> read XML document -> as events

are triggered, handler is executed -> Parsing halted

Page 10: PHP and XML

XMLReader (1.2.b)

• XMLReader:– Forward-only cursor on XML documents, stopping

at each node in it– Pull parser - User controls progress through the

document and decide whether information should be retrieved from the current node

– Small API, faster processing, offers streaming validation and namespaces support etc.

Page 11: PHP and XML

XSL Extension (2)

• XSL extension:– XML-based style sheet, used to transform XML

documents into another XML documents– PHP5 offers new XSL extension that separated

DOM and XSL extension (but still dependant upon DOM)

– Able to execute PHP and use resulting data within the transformation

Page 12: PHP and XML

Data Exchange and Web Service (3)

• Using XML for exchanging data and integrating systems– Three native extensions: • Web Distributed Data Exchange (WDDX) • XML Remote Procedural Call (XMLRPC)• Simple Object Access Protocol (SOAP)

Page 13: PHP and XML

libxml Extension (4)

• PHP5 libxml extension:– Serves as center of common functionality shared

across all XML-based extensions– Used libxml2 as its backend

Page 14: PHP and XML

SIMPLEXMLXML Parser

Page 15: PHP and XML

SimpleXML

• SimpleXML – PHP extension to manipulate XML data– Available only in PHP5– Relatively simple compared to DOM– Converts XML into object:• Elements – converted to single attributes of the

SimpleXMLElement object• Attributes – accessed as associative arrays• Element data – converted to strings

Page 16: PHP and XML

SimpleXML FunctionsFunction Description

__construct() Creates a new SimpleXMLElement objectaddAttribute() Adds an attribute to the SimpleXML elementaddChild() Adds a child element the SimpleXML elementasXML() Gets an XML string from a SimpleXML elementattributes() Gets a SimpleXML element's attributeschildren() Gets the children of a specified nodegetDocNamespaces() Gets the namespaces of an XML documentgetName() Gets the name of a SimpleXML elementgetNamespace() Gets the namespaces from XML dataregisterXPathNamespace() Creates a namespace context for the next XPath querysimplexml_import_dom() Gets a SimpleXMLElement object from a DOM nodesimplexml_load_file() Gets a SimpleXMLElement object from an XML documentsimplexml_load_string() Gets a SimpleXMLElement object from an XML stringxpath() Runs an XPath query on XML data

Page 17: PHP and XML

Creating SimpleXML Element• Instantiated SimpleXML element

• Using asXML() method – output document/subtree to string/file

$xml = "<library><book>XML and PHP</book></library>";$sxe = new SimpleXMLElement($xml);

$xml = "<library><book>XML and PHP</book></library>";$sxe = new SimpleXMLElement($xml);print $sxe->asXML(); //output in browser$sxe->asXML(“test.xml”); //output to file

Page 18: PHP and XML

XML from database• Use provided SQL statement to create table and some data• Create PHP code to create XML

$sql = "SELECT * FROM books";$query = mysql_query($query) or die(mysql_error());$xml = "<library>";

while($row = mysql_fetch_array($query)){$xml .= "<book>";$xml .= "<id>".$row['id']."</id>";$xml .= "<title>".$row['title']."</title>";$xml .= "<author>".$row['author']."</author>";  $xml .=

"<description>".$row['description']."</description>";    $xml .= "<on_sale>".$row['on_sale']."</on_sale>";    $xml .= "</book>";  }

$xml .= "</library>";$sxe = new SimpleXMLElement($xml);$sxe->asXML("test.xml");

Page 19: PHP and XML

Accessing element• To access elements of the XML tree by its name (using

“kereta.xml”):

<?php$katalog = simplexml_load_file(“kereta.xml");print_r($katalog); //prints XML tree as array

print $katalog->kereta->model; //prints first car model in XML

foreach ($katalog->kereta as $car) {echo $car->model;echo '<br/>';

} //prints all the car model in XML

echo $katalog->kereta[4]->model; //prints “Mazda 3 Sedan”

Page 20: PHP and XML

Accessing unknown element• To access unknown elements of the XML tree, use children()

method:

<?php$katalog = simplexml_load_file(“kereta.xml");print_r($katalog->children()); //prints XML tree

foreach ($katalog->children() as $car) {echo $car->model;echo '<br/>';

} //prints all the car model in XML

Page 21: PHP and XML

Extracting part of XML• To create XML document from a part of another XML

document, combine both SimpleXMLElement and asXML:

<?php$katalog = simplexml_load_file(“kereta.xml");$katalog->kereta[4]->asXML(“kereta4.xml”);

Page 22: PHP and XML

The End – Thank You


Recommended