XML
  Home arrow XML arrow Page 3 - Using PHP with XML (part 2)
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Developerworks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

Using PHP with XML (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2001-06-20

    Table of Contents:
  • Using PHP with XML (part 2)
  • Meet Joe Cool
  • Building A Library
  • Anyone For Chicken?
  • Conclusions...
  • ...And Links

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Using PHP with XML (part 2) - Building A Library
    (Page 3 of 6 )

    Using this information, it's pretty easy to re-create our first example using the DOM parser. Here's the XML data,

    <?xml version="1.0"?> <library> <book> <title>Hannibal</title> <author>Thomas Harris</author> <genre>Suspense</genre> <pages>564</pages> <price>8.99</price> <rating>4</rating> </book> <book> <title>Run</title> <author>Douglas E. Winter</author> <genre>Thriller</genre> <pages>390</pages> <price>7.49</price> <rating>5</rating> </book> <book> <title>The Lord Of The Rings</title> <author>J. R. R. Tolkien</author> <genre>Fantasy</genre> <pages>3489</pages> <price>10.99</price> <rating>5</rating> </book> </library>
    and here's the script which does all the work.

    <html> <head> <title>The Library</title> <style type="text/css"> TD {font-family: Arial; font-size: smaller} H2 {font-family: Arial} </style> </head> <body bgcolor="white"> <h2>The Library</h2> <table border="1" cellspacing="1" cellpadding="5"> <tr> <td align=center>Title</td> <td align=center>Author</td> <td align=center>Price</td> <td align=center>User Rating</td> </tr> <? // text ratings $ratings = array("Words fail me!", "Terrible", "Bad", "Indifferent", "Good", "Excellent"); // data file $file = "library.xml"; // create a document object $dom = xmldocfile($file); // get reference to root node $root = $dom->root(); // array of root node's children - the <book> level $nodes = $root->children(); // iterate through <book>s for ($x=0; $x<sizeof($nodes); $x++) { // new row echo "<tr>"; // check type // this is to correct whitespace (empty nodes) if ($nodes[$x]->type == XML_ELEMENT_NODE) { $thisNode = $nodes[$x]; // get an array of this node's children - the <title>, <author> level $childNodes = $thisNode->children(); // iterate through children for($y=0; $y<sizeof($childNodes); $y++) { // check type again if ($childNodes[$y]->type == XML_ELEMENT_NODE) { // appropriate markup for each type of tag // like a switch statement if ($childNodes[$y]->name == "title") { echo "<td><i>" . $childNodes[$y]->content . "</i></td>"; } if ($childNodes[$y]->name == "author") { echo "<td>" . $childNodes[$y]->content . "</td>"; } if ($childNodes[$y]->name == "price") { echo "<td>$" . $childNodes[$y]->content . "</td>"; } if ($childNodes[$y]->name == "rating") { echo "<td>" . $ratings[$childNodes[$y]->content] . "</td>"; } } } } // close the row tags echo "</tr>"; } ?> </table> </body> </html>
    This may appear complex, but it isn't really all that hard to understand. I've first obtained a reference to the root of the document tree, $root, and then to the "children" of that root; these children are structured as elements of a regular PHP array. I've then used a "for" loop to iterate through the array, navigate to the next level, and print the content found in the nodes, with appropriate formatting.

    The numerous "if" loops you see are needed to check the name of each node, and format it appropriately; in fact, they're equivalent to the "switch" statements used in the previous article.

    More XML Articles
    More By icarus, (c) Melonfire


     

       

    XML ARTICLES

    - How to Set Up Podcasting and Vodcasting
    - Creating an RSS Reader Application
    - Building an RSS File
    - An Introduction to XUL Part 6
    - An Introduction to XUL Part 5
    - An Introduction to XUL Part 4
    - An Introduction to XUL Part 3
    - An Introduction to XUL Part 2
    - An Introduction to XUL Part 1
    - XML Matters: Practical XML Data Design and M...
    - Practical XML Data Design and Manipulation f...
    - SimpleXML
    - XForms Basics, Part 3
    - XForms Basics, Part 2
    - XForms Basics

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway