XML and its applications 4 Processing XML using

  • Slides: 14
Download presentation
XML and its applications: 4. Processing XML using PHP

XML and its applications: 4. Processing XML using PHP

Advantages of XML o open source: platform and vendor independent o structure: human and

Advantages of XML o open source: platform and vendor independent o structure: human and machine readable o family of technologies: xquery, xslt, xpath, xsd, soap etc. o decouple data from application - data lives longer than code (legacy problem) - data first, schema later (pay as you go) o spectrum: unstructured to structured data - potentially all data (evolve model) - potentially all information (data, meta-data, code, documents)

Why XML Processing? Needed for: o domain-specific applications - publishing - web - communication

Why XML Processing? Needed for: o domain-specific applications - publishing - web - communication and networking - security - databases etc. o implementing new generic tools Important constituents: o parsing. XML documents into XML trees o navigatingthrough XML trees o manipulating XML trees o serializing XML trees as XML documents (e. g. DOM Framework)

XML Processing Models Pull & Push: o pull: i) Tree oriented parsers - where

XML Processing Models Pull & Push: o pull: i) Tree oriented parsers - where the document is read into memory and converted to a tree structure by the parser and processing flow is controlled by the application; (e. g. Simple. XML extension) ii) streaming parsers – reads the document in iterative mode – processing can begin before the parser has finished reading the document (e. g XMLReader extension in PHP 5. 2 on) o push: where the document is traversed in node order and processing events are fired by the application when some condition is met e. g. the beginning or end of a node; the application has no control to request events instead they are passed by the parser when available; the most popular push based API is the Simple API for XML [SAX 2]

Document Object Model (DOM) A specification for a programming interface (API) from the W

Document Object Model (DOM) A specification for a programming interface (API) from the W 3 C (currently at level 3) "The Document Object Model is a platform- and language-neutral interface that will allow programs and scripts to dynamically access and update the content, structure and style of documents. The document can be further processed and the results of that processing can be incorporated back into the presented page. ” (W 3 C) ext/dom library provides DOM, Schema, Relax. NG, XPath & XPointer functionality of the libxml 2 library in php 5

DOM example with PHP 5 <? php $wines = new DOMDocument(); $wines->load( 'wines. xml'

DOM example with PHP 5 <? php $wines = new DOMDocument(); $wines->load( 'wines. xml' ); wines. xml source $categorys = $wines->get. Elements. By. Tag. Name("category"); foreach( $categorys as $category ) { echo "<p>"; $name = $category->get. Attribute("name"); echo "<b>Type: $name</b><br/>"; $products = $category->get. Elements. By. Tag. Name("product"); foreach( $products as $product ) { $titles = $product->get. Elements. By. Tag. Name( "title" ); $title = $titles->item(0)->node. Value; echo "$title<br/>"; } echo "</p>"; } ? > run the code

Simple. XML extension in PHP 5 DOM is full featured and allows for the

Simple. XML extension in PHP 5 DOM is full featured and allows for the manipulation of XML in a wide variety of ways but is quite complex. It is a large API, requiring a developer to really understand all the intricate details of working with XML. The Simple. XML extension is much simpler and takes an Object Oriented (OO) approach. It views the xml document as an object. Elements are represented as properties and attributes as accessors. It includes full iterator support and allows for the use of the “foreach” operator. It can use the DOM when required (e. g. for writing XML files) and has built in XPath support. Simple. XML works best with uncomplicated, record-like data, such as XML passed as a document or string from another internal part of the same application. Provided that the XML document isn't too complicated, too deep, and lacks mixed content, Simple. XML is easier to code than the DOM.

Simple. XML extension in PHP 5 <? php $wines = simplexml_load_file('wines. xml'); foreach ($wines->category

Simple. XML extension in PHP 5 <? php $wines = simplexml_load_file('wines. xml'); foreach ($wines->category as $category) { echo "<p>"; echo "<b>Type: ". $category["name"]. "</b><br/>"; foreach ($category->product as $product) { echo $product->title, ' '; } echo "</p>"; } ? > run the code

Using XPath to search documents Sometimes there is a need to search through a

Using XPath to search documents Sometimes there is a need to search through a document (e. g. when the exact format or order of the nodes is not known in advance) or when only a small set of nodes need to be found in a very large document. Using the Xpath support built into the Simple. XML extension this is made easy. The basic format of a XPath query takes the form “a/b/c” where a, b, c are nested xml tags of the form <a><b><c/></b></a>. Some common XPath queries are as follows: a : matches any tag named a a/b : matches any tag named b, directly contained in the tag a a/b/. . : matches and returns the tag a instaed of b a//b : matches b when it is any decendent of a a[31] : matches the 31 st a tag a[last()] : matches the very last a tag a[@att] : matches any a with an attribute named “att” a[@att=“val”] matches any tag called a with an attribute named “att” with the value “val”

Simple. XML & XPath example <? php $wines = simplexml_load_file('wines. xml'); $category = $wines->xpath('//category');

Simple. XML & XPath example <? php $wines = simplexml_load_file('wines. xml'); $category = $wines->xpath('//category'); foreach ($category as $c) { echo "<p>"; echo "<b>Type: {$c['name']}</b><br/>"; $title = $c->xpath('. /product/title'); foreach ($title as $t) { echo "{$t}<br/>"; } echo "</p>"; } ? > run the code

XSLT processing using PHP <? php // create a new xslt processor $xp =

XSLT processing using PHP <? php // create a new xslt processor $xp = new Xslt. Processor(); // create a DOM document and load the XSL stylesheet $xsl = new Dom. Document; $xsl->load('patient. xslt'); // import the XSL styelsheet into the XSLT process $xp->import. Stylesheet($xsl); // create a DOM document and load the XML datat $xml_doc = new Dom. Document; $xml_doc->load('patient. xml'); // transform the XML into HTML using the XSL file if ($html = $xp->transform. To. XML($xml_doc)) { echo $html; } else { trigger_error('XSL transformation failed. ', E_USER_ERROR); } ? > run the code

Validating XML using the DOM <? php $dom = new DOMDocument; $dom->load('wines. xml'); if

Validating XML using the DOM <? php $dom = new DOMDocument; $dom->load('wines. xml'); if (!$dom->schema. Validate('wines. xsd')) { echo 'Document is not valid'; } else { echo 'Document is valid'; } ? > run the code

Pull Parsing with XMLReader PHP 5 introduced XMLReader, a new class for parsing XML.

Pull Parsing with XMLReader PHP 5 introduced XMLReader, a new class for parsing XML. Unlike Simple. XML or the Document Object Model (DOM), XMLReader operates in streaming mode. That is, it reads the document from start to finish. Processing can start before the parser has finished reading the document. Unlike the Simple API for XML (SAX), XMLReader is a pull parser rather than a push parser. Hence the processing program is in control. Rather than being told what the parser sees when the parser sees it, the program tells the parser when to go fetch the next piece of the document. The program requests content rather than reacts to it. Another way of thinking about it: XMLReader is an implementation of the Iterator design pattern rather than the Observer design pattern.

Pull Parsing with XMLReader Example <? php $reader = new XMLReader(); $reader->open('wines. xml'); while

Pull Parsing with XMLReader Example <? php $reader = new XMLReader(); $reader->open('wines. xml'); while ($reader->read()) { echo "<p>"; if ($reader->node. Type == XMLREADER: : ELEMENT) { if ($reader->local. Name=="category") { echo "<b>". $reader->get. Attribute("name"). "</b>"; } if ($reader->local. Name=="title") { $reader->read(); echo $reader->value; } } echo "</p>"; } ? > run the code