PHP
  Home arrow PHP arrow Page 4 - 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 - 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:

    Retrieving attributes is easy with DOMXML

    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.

    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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek