PHP
  Home arrow PHP arrow Page 5 - Parsing XML With DOMXML And PHP
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Parsing XML With DOMXML And PHP
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 18
    2002-01-10

    Table of Contents:
  • Parsing XML With DOMXML And PHP
  • What is the DOMXML parsing method?
  • Parsing a simple XML document
  • Parsing Example 2
  • Putting our DOMXML knowledge to good use
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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:

    Our sample PHP/DOMXML script in action!

    To see our XML parsing PHP script in action, you can click here

    More PHP Articles
    More By Mitchell Harper


     

    PHP ARTICLES

    - 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
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek