Home arrow XML arrow Page 3 - Using PHP with XML (part 2)

Building A Library - XML

PHP offers two methods of parsing an XML document. You've already seen how SAX works; in this article, find out how to use PHP's DOM functions to traverse an XML tree, and also learn about important differences between the two approaches.

TABLE OF CONTENTS:
  1. Using PHP with XML (part 2)
  2. Meet Joe Cool
  3. Building A Library
  4. Anyone For Chicken?
  5. Conclusions...
  6. ...And Links
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 3
June 20, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

XML ARTICLES

- Google Docs and Xpath Data Functions
- Flex Array Collection Sort and Filtering
- The Flex Tree Control
- Flex List Controls
- Working with Flex and Datagrids
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: