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.
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.