Presentation is loading. Please wait.

Presentation is loading. Please wait.

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)

Similar presentations


Presentation on theme: "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)"— 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: 1.Parsers - tree-based (DOM, SimpleXML) and streaming (SAX, XMLReader) parsers 2.XSL Extension 3.Data Exchange & Web Services 4.libxml extension

4 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

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 SIMPLEXML XML Parser

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 = " XML and PHP "; $sxe = new SimpleXMLElement($xml); $xml = " XML and PHP "; $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 = " "; while($row = mysql_fetch_array($query)){ $xml.= " "; $xml.= " ".$row['id']." "; $xml.= " ".$row['title']." "; $xml.= " ".$row['author']." "; $xml.= " ".$row['description']." "; $xml.= " ".$row['on_sale']." "; $xml.= " "; } $xml.= " "; $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 ' '; } //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 ' '; } //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


Download ppt "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)"

Similar presentations


Ads by Google