A Sneak Preview of XML in PHP 5

  • Slides: 16
Download presentation
A Sneak Preview of XML in PHP 5 Adam Trachtenberg Co-Author PHP Cookbook adam@trachtenberg.

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

XML Extensions in PHP 5 • Work together as a unified whole. • Are

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

Five Major Extensions • • • DOM SAX Simple. XML XPath XSLT Adam Trachtenberg

Five Major Extensions • • • DOM SAX Simple. XML XPath XSLT Adam Trachtenberg [http: //www. trachtenberg. com]

Music Catalogue XML <music> <artist id="1"> <name>The Rolling Stones</name> <albums> <title>Exile On Main Street</title>

Music Catalogue XML <music> <artist id="1"> <name>The Rolling Stones</name> <albums> <title>Exile On Main Street</title> </albums> </artist> <artist id="2"> <name>Aimee Mann</name> <albums> <title>I'm With Stupid</title> <title>Bachelor No. 2</title> </albums> </artist> </music> Adam Trachtenberg [http: //www. trachtenberg. com]

DOM • The 800 pound gorilla of XML. • You can do everything and

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

DOM Example Code $music = new dom. Document; $music->preserve. White. Space = false; $music->load('music.

DOM Example Code $music = new dom. Document; $music->preserve. White. Space = false; $music->load('music. xml'); $names = $music-> get. Elements. By. Tag. Name('name'); foreach ($names as $name) { print $name->first. Child ->node. Value; } The Rolling Stones Aimee Mann Adam Trachtenberg [http: //www. trachtenberg. com]

SAX • PHP's original XML extension. • Streaming (or event-based) parser • Uses less

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 libxml 2 Adam Trachtenberg [http: //www. trachtenberg. com]

Simple. XML • A new PHP 5 only extension. • Excels at parsing RSS

Simple. XML • 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, Simple. XML is the way to go. • Only supports elements, text nodes, and attributes. • Tree-based API Adam Trachtenberg [http: //www. trachtenberg. com]

Simple. XML Example Code $x = simplexml_load_string($xml); $artists = $x->artist; foreach($artists as $artist) {

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

XPath • Query XML documents like you're using regular expressions to find the subset

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 Simple. XML Adam Trachtenberg [http: //www. trachtenberg. com]

XPath Example Code $xpath = new dom. XPath($music); $albums = $xpath->query( "/music/artist[name = 'Rolling

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

XSLT • A way to take XML documents and transform them into HTML or

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. Adam Trachtenberg [http: //www. trachtenberg. com]

XSLT Example Code $xslt = new xslt. Processor; $xsl = dom. Document: : load('music.

XSLT Example Code $xslt = new xslt. Processor; $xsl = dom. Document: : load('music. xsl'); $xslt->import. Stylesheet($xsl); $xml = dom. Document: : load('music. xml'); print $xslt->transform. To. XML($xml); Adam Trachtenberg [http: //www. trachtenberg. com]

Web Services • SOAP – PEAR: : SOAP – soap Extension (Written in C)

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

Resources • DOM, XSLT, XPath – PHP 5 Meets XML and the DOM PHP

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

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

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