1 / 22

PHP and XML

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.

caron
Download Presentation

PHP and XML

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP and XML TP2653 Advance Web Programming

  2. 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.

  3. Core XML Extensions • Core XML Extensions: • Parsers - tree-based (DOM, SimpleXML) and streaming (SAX, XMLReader) parsers • XSL Extension • Data Exchange & Web Services • libxml extension

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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.

  11. 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

  12. 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)

  13. libxml Extension (4) • PHP5 libxml extension: • Serves as center of common functionality shared across all XML-based extensions • Used libxml2 as its backend

  14. XML Parser SIMPLEXML

  15. 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

  16. SimpleXML Functions

  17. 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

  18. 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");

  19. 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”

  20. 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

  21. 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”);

  22. The End – Thank You

More Related