PHP
  Home arrow PHP arrow Page 2 - Parsing Child Nodes with the DOM XML extension in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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? 
Google.com  
PHP

Parsing Child Nodes with the DOM XML extension in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2008-04-08


    Table of Contents:
  • Parsing Child Nodes with the DOM XML extension in PHP 5
  • Reintroducing a few methods of the DOM XML extension
  • Working with the hasChildNodes() method
  • The removeChild() method

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 Child Nodes with the DOM XML extension in PHP 5 - Reintroducing a few methods of the DOM XML extension
    ( Page 2 of 4 )

    Before you start learning a brand new pair of methods aimed at handling the child nodes of a specified XML document, first I’d like to reintroduce some methods that were discussed in the last tutorial of the series. This way, you can fill some gaps regarding their concrete utilization.

    Below I included two basic hands-on examples that demonstrate how to use the “getAttribute()” and “has Attribute()” methods in order to work with the respective attributes of a sample “headlines.xml” file:

    Here are the corresponding code samples, along with the definition of the mentioned XML file. So take a close look at them, please:


    // definition of 'headlines.xml' file


    <?xml version="1.0" encoding="iso-8859-1"?>

    <headlines>

    <headline id="economics">

    <image>image1.jpg</image>

    <url>Link for headline 1 goes here</url>

    <text>Text for headline 1 goes here</text>

    </headline>

    <headline id="sports">

    <image>image2.jpg</image>

    <url>Link for headline 2 goes here</url>

    <text>Text for headline 2 goes here</text>

    </headline>

    <headline id="jetset">

    <image>image3.jpg</image>

    <url>Link for headline 3 goes here</url>

    <text>Text for headline 3 goes here</text>

    </headline>

    <headline id="technology">

    <image>image4.jpg</image>

    <url>Link for headline 4 goes here</url>

    <text>Text for headline 4 goes here</text>

    </headline>

    <headline id="art">

    <image>image5.jpg</image>

    <url>Link for headline 5 goes here</url>

    <text>Text for headline 5 goes here</text>

    </headline>

    </headlines>


    // example on using the 'getAttribute()' method


    $dom=new DOMDocument();

    $dom->load('headlines.xml');

    $headlines=$dom->getElementsByTagName('headline');

    foreach($headlines as $headline){

    echo 'ID attribute of current node is the following: '.$headline->getAttribute('id').'<br />';

    }


    /* displays the following

    ID attribute of current node is the following: economics

    ID attribute of current node is the following: sports

    ID attribute of current node is the following: jetset

    ID attribute of current node is the following: technology

    ID attribute of current node is the following: art

    */ 


    // example on using the 'hasAttribute()' method

    $dom=new DOMDocument();

    $dom->load('headlines.xml');

    $headlines=$dom->getElementsByTagName('headline');

    foreach($headlines as $headline){

    if($headline->hasAttribute('id')){

    echo 'ID attribute of current node is the following: '.$headline->getAttribute('id').'<br />';

    }

    }


    /* displays the following

    ID attribute of current node is the following: economics

    ID attribute of current node is the following: sports

    ID attribute of current node is the following: jetset

    ID attribute of current node is the following: technology

    ID attribute of current node is the following: art

    */


    As demonstrated by the previous examples, checking for the existence of a particular attribute that belongs to a specific XML node and getting its corresponding value is actually a no-brainer process that can be accomplished with minor problems by using the “hasAttribute()” and “getAttribute()” methods shown above.

    In this case, the methods in question are used to parse the node attributes of a trivial XML file, but of course, the same business logic can be applied when working with more complex XML data.

    With the two earlier code examples well underway, this is an appropriate moment to continue exploring some other helpful features offered by the DOM XML extension. So in accordance with the concepts that I deployed in the introduction, in the next section of this article, I’m going to show you how to determine whether or not a specified node within an XML document has child nodes.

    As you might have guessed, to see the complete details about how this will be achieved, simply click on the linked title that appears below and keep reading.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





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