Parsing XML With DOMXML And PHP - Parsing Example 2
(Page 4 of 6 )
In example one, the elements in our XML document didn't contain any attributes. Let's parse some XML that contains attributes:
<?php
$theXML = "<?xml version='1.0'?>";
$theXML .= " <people>";
$theXML .= " <person id='13' dob='13/07/82'>";
$theXML .= " Mitchell Harper";
$theXML .= " </person>";
$theXML .= " <person id='14' dob='20/08/1960'>";
$theXML .= " Bodger Dodger";
$theXML .= " </person>";
$theXML .= " </people>";
$xmlDoc = xmldoc($theXML);
$root = $xmlDoc->root();
$people = $root->children();
while($person = array_shift($people))
{
if($person->name == "person")
{
echo "<b>Person Found:</b><br> ";
echo "Name: " . $person->content . "<br>";
echo "Id: " . $person->getattr("id") . "<br>";
echo "DOB: " . $person->getattr("dob") . "<br><br>";
}
}
?>If you copy-paste this code into a file named "example2.php" and run it through your browser, you'll get the following output:

As you can see, our XML string contains the details of two people, each persons details were stored in a <person> element.
Parsing the XML is a little different this time. We load and parse the XML string as normal, however we assign each child node of the documents root element, <people>, to an associative array in the $people variable:
$xmlDoc = xmldoc($theXML);
$root = $xmlDoc->root();
$people = $root->children();We use the $people associative array to loop through each of the root elements classes that it contains. The "name" key/value pair returns the name of the current node that we are working with. We only want to work with <person> nodes, so we use a simple "if" block to take care of that:
if($person->name == "person")Each <person> node contains two attributes and a child text node, which contains the person's name. To extract the person's id and date of birth attributes, we use the getattr() function, passing it the name of the attribute whose value we're after:
echo "Id: " . $person->getattr("id") . "<br>";
echo "DOB: " . $person->getattr("dob") . "<br><br>";One import thing to note about the DOMXML parser is that it's very fussy with spaces. For example, if we had this XML string:
<blah> <node/> </blah>Then each space between the nodes would be treated as a text node, thus our XML document would contain five elements instead of three. To correct this, just remove the spaces between each node:
<blah><node/></blah>As you can see from the two examples described above, XML nodes are represented as a set of associative arrays and classes through the DOMXML library. Before we move onto the next section however, I strongly suggest that you take a look at
this link. It's is part of PHP's manual and contains a tonne of information about using DOMXML from within PHP, including how to create and append nodes, how to access class attributes and how to instantiate new DOMXML classes programmatically.
Next: Putting our DOMXML knowledge to good use >>
More PHP Articles
More By Mitchell Harper