Extensible Mark-up Language (XML) has just about made its impression everywhere: B2B, B2C, as a replacement for .INI files, to aid with data abstraction and transportation, etc. In this article Mitchell shows us how to use the DOMXML library to parse and extract data from both local and remote XML files with PHP.
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:
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:
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.