240 likes | 417 Views
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.
E N D
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 • Core XML Extensions: • Parsers - tree-based (DOM, SimpleXML) and streaming (SAX, XMLReader) parsers • XSL Extension • Data Exchange & Web Services • libxml extension
Parsing XML using PHP (1) • PHP5 supports XML parsing better than PHP4 • XML parsers in PHP5: simplexml DOM xml xmlreader • Easy object-oriented tree-based XML parsing • Modeled after Perl’s XML:Simple • Object-oriented tree-based XML navigation, creation and modification • SAX2 event-based parsing • Backward compatible with PHP4 parser • Stream-oriented, forward only XML parsing • Based on MS System.XML. XMLReader
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
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
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
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
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
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.
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
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)
libxml Extension (4) • PHP5 libxml extension: • Serves as center of common functionality shared across all XML-based extensions • Used libxml2 as its backend
XML Parser SIMPLEXML
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
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
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");
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”
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
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”);