PHP
  Home arrow PHP arrow Page 3 - 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 a simple XML document


    (Page 3 of 6 )

    Example One

    Consider the following XML document:

    <?xml version="1.0"?>

    <website>

    <url>http://www.devarticles.com</url>

    </website>


    To extract the contents of the <url> node, we would use the following PHP code:

    <?php

    $theXML = "<?xml version='1.0'?>";

    $theXML .= " <website><url>";

    $theXML .= " http://www.devarticles.com";

    $theXML .= " </url></website>";

    $xmlDoc = xmldoc($theXML);

    $root = $xmlDoc->root();

    $node = array_shift($root->children());

    echo $node->content;

    ?>


    We start by creating a string that contains our XML. The xmldoc() function creates an object that contains our hierarchically structured and validated XML data. If our XML was not valid, then the xmldoc() function would spit an error, like this:

    The xmldoc() function spits an error message

    One of the great things about using the DOMXML parser is that you can just as easily load a local or remote file, using the xmldocfile() function instead:

    $xmlDoc = xmldocfile("/htdocs/xml/sample.xml");

    OR

    $xmlDoc = xmldocfile("http://www.mysite.com/sample.xml");

    All XML documents have a single root element, which is the starting point of the documents content. In our example, it's the <website> element. We retrieve the root element with the root() function, like this:

    $root = $xmlDoc->root();

    Once we have access to the root element, we use its children() function to return a list of its child nodes as an associative array. The array_shift() function pops the first item off the array and returns it as an associative array. Its "content" key/value pair will contain the URL that we're after, and we use the echo command to write it to the browser window:

    $node = array_shift($root->children());

    echo $node->content;

    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