Home arrow PHP arrow Page 5 - Parsing XML With DOMXML And PHP

Parsing XML With DOMXML And PHP

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.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 27
January 10, 2002
  1. · Parsing XML With DOMXML And PHP
  2. · What is the DOMXML parsing method?
  3. · Parsing a simple XML document
  4. · Parsing Example 2
  5. · Putting our DOMXML knowledge to good use
  6. · Conclusion

print this article

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 its 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 youd 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:



case "title":


$title = $nodeArray[$i]->content;



case "url":


$url = $nodeArray[$i]->content;



case "doc_type":


$docType = $nodeArray[$i]->content;



case "summary":


$summary = $nodeArray[$i]->content;




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:


<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; ?>





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:

Our sample PHP/DOMXML script in action!

To see our XML parsing PHP script in action, you can click here
blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials