XML and PHP 5


Published on

1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

XML and PHP 5

  1. 1. A Sneak Preview of XML in PHP 5 Adam Trachtenberg Co-Author PHP Cookbook [email_address] January 21, 2004
  2. 2. XML Extensions in PHP 5 <ul><li>Work together as a unified whole. </li></ul><ul><li>Are standardized on a single XML library: libxml2 . </li></ul><ul><li>Fully comply with W3 specifications. </li></ul><ul><li>Efficiently process data. </li></ul><ul><li>Provide you with the right XML tool for your job. </li></ul>
  3. 3. Five Major Extensions <ul><li>DOM </li></ul><ul><li>SAX </li></ul><ul><li>SimpleXML </li></ul><ul><li>XPath </li></ul><ul><li>XSLT </li></ul>
  4. 4. Music Catalogue XML <ul><li><music> </li></ul><ul><li><artist id=&quot;1&quot;> </li></ul><ul><li><name> The Rolling Stones </name> </li></ul><ul><li><albums> </li></ul><ul><li><title> Exile On Main Street </title> </li></ul><ul><li></albums> </li></ul><ul><li></artist> </li></ul><ul><li><artist id=&quot;2&quot;> </li></ul><ul><li><name> Aimee Mann </name> </li></ul><ul><li><albums> </li></ul><ul><li><title> I'm With Stupid </title> </li></ul><ul><li><title> Bachelor No. 2 </title> </li></ul><ul><li></albums> </li></ul><ul><li></artist> </li></ul><ul><li></music> </li></ul>
  5. 5. DOM <ul><li>The 800 pound gorilla of XML. </li></ul><ul><li>You can do everything and the <kitchen:sink> with DOM, but navigating through your documents can be cumbersome. </li></ul><ul><li>Tree-based API </li></ul><ul><li>Undergone a complete rewrite to correspond with DOM specifications. </li></ul>
  6. 6. 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. 7. SAX <ul><li>PHP's original XML extension. </li></ul><ul><li>Streaming (or event-based) parser </li></ul><ul><li>Uses less memory than DOM, but frequently at the expense of more complex PHP code. </li></ul><ul><li>Used to use expat. Now uses libxml2 </li></ul>
  8. 8. SimpleXML <ul><li>A new PHP 5 only extension. </li></ul><ul><li>Excels at parsing RSS files, REST results, and configuration data. </li></ul><ul><li>If you know the document's format ahead of time, SimpleXML is the way to go. </li></ul><ul><li>Only supports elements, text nodes, and attributes. </li></ul><ul><li>Tree-based API </li></ul>
  9. 9. SimpleXML Example Code $x = simplexml_load_string ( $xml ); $artists = $x -> artist ; foreach( $artists as $artist ) {      print &quot;$artist->name &quot; ; } The Rolling Stones Aimee Mann
  10. 10. XPath <ul><li>Query XML documents like you're using regular expressions to find the subset of information that you need and eliminate the unnecessary portions. </li></ul><ul><li>Highly useful and undervalued piece of XML. </li></ul><ul><li>Usable with DOM and SimpleXML </li></ul>
  11. 11. XPath Example Code <ul><li>$xpath = new domXPath ( $music ); </li></ul><ul><li>$albums = $xpath -> query ( </li></ul><ul><li>&quot;/music/artist[name = </li></ul><ul><li>'Rolling Stones']/albums/name&quot; ); </li></ul><ul><li>foreach ( $albums as $a ) </li></ul><ul><li>print $a -> firstChild -> nodeValue ; </li></ul><ul><li>} </li></ul><ul><li>Exile On Main Street </li></ul>
  12. 12. XSLT <ul><li>A way to take XML documents and transform them into HTML or another output format. </li></ul><ul><li>Defines templates using XML-based stylesheets. </li></ul><ul><li>Easily shared among different applications, but has a quirky syntax. </li></ul><ul><li>Uses libxslt library instead of Sablotron. </li></ul>
  13. 13. XSLT Example Code <ul><li>$xslt = new xsltProcessor ; </li></ul><ul><li>$xsl = domDocument :: load ( 'music.xsl' ); </li></ul><ul><li>$xslt -> importStylesheet ( $xsl ); </li></ul><ul><li>$xml = domDocument :: load ( 'music.xml' ); </li></ul><ul><li>print $xslt -> transformToXML ( $xml ); </li></ul>
  14. 14. Web Services <ul><li>SOAP </li></ul><ul><ul><li>PEAR::SOAP </li></ul></ul><ul><ul><li>soap Extension (Written in C) </li></ul></ul><ul><li>REST </li></ul><ul><ul><li>SimpleXML </li></ul></ul><ul><li>XML-RPC </li></ul><ul><ul><li>Still there… </li></ul></ul>
  15. 15. Resources <ul><li>DOM, XSLT, XPath </li></ul><ul><ul><li>PHP 5 Meets XML and the DOM PHP Magazine: www.php-mag.net </li></ul></ul><ul><li>SimpleXML </li></ul><ul><ul><li>Using PHP 5's SimpleXML O’Reilly Network: www.onlamp.com/php </li></ul></ul>
  16. 16. Did I mention I wrote a book?