PHP
  Home arrow PHP arrow Page 3 - 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 - Working with the hasChildNodes() method
    ( Page 3 of 4 )

    So far, you learned how to use the functionality provided by the DOM XML extension to iterate over a certain number of nodes contained within a simple XML document and retrieve their respective attributes. Nonetheless, as I suggested before, the library has a few additional methods that permit you to work directly with the child nodes of a specified element, as you’ve probably done hundreds of times, when using JavaScript to manipulate web documents on the client-side.

    Speaking more specifically, the first method that I’m going to show you, with regard to handling child nodes, is one called “hasChildNodes().” It is tasked with determining whether a concrete node of an XML document also contains sub nodes (or child elements).

    To illustrate how the “hasChildNodes()” does its thing, first I’m going to include the definition of the sample “headlines.xml” file that you saw in the previous section. Here it is:


    // 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>


    Now that you've recalled how the above XML file looks, let me go one step further and show you a short script that traverses the file in question and checks to see if each of its <headline> elements has a child node or not.

    The signature of this brand new script is the following:


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


    $dom=new DOMDocument();

    // load XML data from existing file

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

    // get <headline> nodes

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

    foreach($headlines as $headline){

    if($headline->hasChildNodes()){

    echo 'This node has children!<br />';

    }

    }


    /* displays the following

    This node has children!

    This node has children!

    This node has children!

    This node has children!

    This node has children!

    */


    Despite the simplicity of the above code sample, I’m reasonably sure that it’s been useful enough to demonstrate the logic that drives the “hasChildNodes()” method. On this specific occasion, since each <headline> element within the pertinent XML document does wrap a group of child nodes, this condition is reflected by echoing an indicative message on the browser. Grasping how this method does its thing definitely isn’t rocket science.

    Well, at this point you should congratulate yourself, since you learned one more method in the vast arsenal provided by the DOM XML extension. Nonetheless, if you're used to working with XML documents on a frequent basis, then it’s highly possible that you’re wondering if this extension has the capability to remove an element’s child node. Luckily, the answer is an emphatic yes! And this will be the last topic that I’m going to discuss in this tutorial.

    Deleting a child node from a given XML document is a process that must be performed by way of yet another method of the DOM XML extension, whose name is “removeChild().”

    To see how it will be implemented in the context of a concrete example, please click on the link below and keep reading. We’re almost done!



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