Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Sneak Preview of XML in PHP 5 Adam Trachtenberg Co-Author PHP Cookbook January 21, 2004.

Similar presentations


Presentation on theme: "A Sneak Preview of XML in PHP 5 Adam Trachtenberg Co-Author PHP Cookbook January 21, 2004."— Presentation transcript:

1 A Sneak Preview of XML in PHP 5 Adam Trachtenberg Co-Author PHP Cookbook adam@trachtenberg.com January 21, 2004

2 Adam Trachtenberg [http://www.trachtenberg.com] XML Extensions in PHP 5 Work together as a unified whole. Are standardized on a single XML library: libxml2. Fully comply with W3 specifications. Efficiently process data. Provide you with the right XML tool for your job.

3 Adam Trachtenberg [http://www.trachtenberg.com] Five Major Extensions DOM SAX SimpleXML XPath XSLT

4 Adam Trachtenberg [http://www.trachtenberg.com] Music Catalogue XML The Rolling Stones Exile On Main Street Aimee Mann I'm With Stupid Bachelor No. 2

5 Adam Trachtenberg [http://www.trachtenberg.com] DOM The 800 pound gorilla of XML. You can do everything and the with DOM, but navigating through your documents can be cumbersome. Tree-based API Undergone a complete rewrite to correspond with DOM specifications.

6 Adam Trachtenberg [http://www.trachtenberg.com] DOM Example Code $music = new domDocument; $music->preserveWhiteSpace = false; $music->load('music.xml'); $names = $music-> getElementsByTagName('name'); foreach ($names as $name) { print $name->firstChild ->nodeValue; } The Rolling Stones Aimee Mann

7 Adam Trachtenberg [http://www.trachtenberg.com] SAX PHP's original XML extension. Streaming (or event-based) parser Uses less memory than DOM, but frequently at the expense of more complex PHP code. Used to use expat. Now uses libxml2

8 Adam Trachtenberg [http://www.trachtenberg.com] SimpleXML A new PHP 5 only extension. Excels at parsing RSS files, REST results, and configuration data. If you know the document's format ahead of time, SimpleXML is the way to go. Only supports elements, text nodes, and attributes. Tree-based API

9 Adam Trachtenberg [http://www.trachtenberg.com] SimpleXML Example Code $x = simplexml_load_string($xml); $artists = $x->artist; foreach($artists as $artist) { print "$artist->name\n"; } The Rolling Stones Aimee Mann

10 Adam Trachtenberg [http://www.trachtenberg.com] XPath Query XML documents like you're using regular expressions to find the subset of information that you need and eliminate the unnecessary portions. Highly useful and undervalued piece of XML. Usable with DOM and SimpleXML

11 Adam Trachtenberg [http://www.trachtenberg.com] XPath Example Code $xpath = new domXPath($music); $albums = $xpath->query( "/music/artist[name = 'Rolling Stones']/albums/name"); foreach ($albums as $a) print $a->firstChild->nodeValue; } Exile On Main Street

12 Adam Trachtenberg [http://www.trachtenberg.com] XSLT A way to take XML documents and transform them into HTML or another output format. Defines templates using XML-based stylesheets. Easily shared among different applications, but has a quirky syntax. Uses libxslt library instead of Sablotron.

13 Adam Trachtenberg [http://www.trachtenberg.com] XSLT Example Code $xslt = new xsltProcessor; $xsl = domDocument::load('music.xsl'); $xslt->importStylesheet($xsl); $xml = domDocument::load('music.xml'); print $xslt->transformToXML($xml);

14 Adam Trachtenberg [http://www.trachtenberg.com] Web Services SOAP –PEAR::SOAP –soap Extension (Written in C) REST –SimpleXML XML-RPC –Still there…

15 Adam Trachtenberg [http://www.trachtenberg.com] Resources DOM, XSLT, XPath –PHP 5 Meets XML and the DOM PHP Magazine: www.php-mag.net SimpleXML –Using PHP 5's SimpleXML O’Reilly Network: www.onlamp.com/php

16 Adam Trachtenberg [http://www.trachtenberg.com] Did I mention I wrote a book?


Download ppt "A Sneak Preview of XML in PHP 5 Adam Trachtenberg Co-Author PHP Cookbook January 21, 2004."

Similar presentations


Ads by Google