Parsing XML With DOMXML And PHP - Putting our DOMXML knowledge to good use
(Page 5 of 6 )
Now that we're versed with some practical ways to parse and extract meaningful data from XML documents, let's take a look at how we can create something useful with what we've learnt.
Open up a new web browser window and go
here. This is an XML file, and it contains a list of the ten most recent articles published on our site. Each <article> element contains the details of one article. I created this XML file due to a lot of public interest in our articles. I had received multiple requests for content automation from our site, and this XML file was the result.
If you're wondering how the XML file is generated, then it’s really quite easy: Every time we add/update an article, a background process named "UpdateXMLDoc" is executed. This process grabs the ten most recent articles from our MySQL database and formats their details into XML using simple string concatenation.
What we're going to do now is create a PHP script that will retrieve this XML file remotely, load it as an XML object and display each article as a hyper linked field in a HTML table. The PHP script is a tad bit big to post in its entirety, however it's available as part of the support material from the last page of this article if you’d like to download it and use it on your site. I'll focus on the more important aspects of the code from here on in.
$xmlDoc = @xmldocfile("http://www.devarticles.com/devarticles_recent.xml") or die("Couldn't get XML data");Firstly, we load the remote XML file using the xmldocfile() function. The function call is prefixed with the "@" symbol, which will stop PHP from spitting errors if they occur. If the function call fails, the die method will write an error to the browser and terminate the script.
$nodeRoot = $xmlDoc->root();
$childNodes = $nodeRoot->children();These two lines should be familiar to you by now. They simply get a reference to the XML documents root element and all of its child nodes as associative arrays.
while($node = array_shift($childNodes))
{
if($node->name == "article")
{Skipping down the script a bit, we load each of the roots child elements and pop them off the array, one at a time. We're only concerned with the <article> nodes, because they contain the information about each article.
for($i = 0; $i < sizeof($nodeArray); $i++)
if($nodeArray[$i]->content <> " ")
{Next, we setup a for loop to run through each <article> node in the XML document. As I mentioned earlier, if you leave spaces between each element, then the space is treated as a text node. We use an if block to skip any blank nodes.
We use a switch command to workout which field we're dealing with in the loop. Each field corresponds to a child node for the <article> node in our XML file. We save each fields value to a temporary variable:
switch($nodeArray[$i]->name)
{
case "title":
{
$title = $nodeArray[$i]->content;
break;
}
case "url":
{
$url = $nodeArray[$i]->content;
break;
}
case "doc_type":
{
$docType = $nodeArray[$i]->content;
break;
}
case "summary":
{
$summary = $nodeArray[$i]->content;
break;
}
}Lastly, we use these variables to form the basis of a HTML <td> tag. I've used a bit of CSS to add some MouseOver creative flair to each <td> tag:
<tr>
<td bgcolor="#FFEFCE" onMouseOver="this.style.backgroundColor='#FFFFC0'" onMouseOut="this.style.backgroundColor='#FFEFCE'">
<p style="margin-top:5; margin-bottom:5; margin-left:10; margin-right:10">
<a target="_blank" href="<?php echo $url; ?>">
<font face="verdana" color="black" size="1">
<?php echo $title; ?>
</font>
</a>
</td>
</tr>This script is complete, and only needs a small amount of work to integrate with any existing site. When I run the script in my browser, it looks like this:

To see our XML parsing PHP script in action, you can
click hereNext: Conclusion >>
More PHP Articles
More By Mitchell Harper