PHP
  Home arrow PHP arrow Page 4 - Parsing Child Nodes with the DOM XML e...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 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 Child Nodes with the DOM XML extension in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    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:
      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

    Minimize the cost of deploying database applications. Advantage Database Server or Microsoft SQL Server – Which One is Right for You? Learn now!

    Parsing Child Nodes with the DOM XML extension in PHP 5 - The removeChild() method
    (Page 4 of 4 )

    If you’ve ever used the DOM API with JavaScript to dynamically remove several elements of a web page, then you’ll find the “removeChild()” method of the DOM XML extension very easy to grasp, since its functionality is identical to that of the one provided by its client-side counter part.

    So, to do things a bit easier and faster, I’m going to use the same “headlines.xml” file that you saw earlier to exemplify how the “removeChild()” method works. That being said, here’s the familiar signature of this basic XML file:


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


    And here’s a simple script that uses the “removeChild()” method to delete the entirety of the <image> elements contained by the parent <headline> nodes.

    The corresponding code sample looks like this:


    // example on removing child nodes using the 'removeChild()' 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){

    while($headline->hasChildNodes()){

    // remove child node

    $headline->removeChild($headline->childNodes->item(0));

    }

    }


    Do you realize how easy it is to delete child nodes from an XML document? I guess you do! As you saw, in the above hands-on example, I used a combination of the “removeChild()” method and the brand new “childNodes” object in order to navigate to the first item of each “<headline> node and delete it.

    Of course, this is merely a basic demonstration of how to implement the “removeChild()” method in a useful manner. But you’re completely free to develop your own testing examples regarding the use of this one and the rest of the methods discussed in this article.

    Final thoughts

    It’s hard to believe, but we’ve come to the end of this series. In general terms, the experience has been instructive and also fun, since you learned how to use some of the most relevant methods that come packaged with the DOM XML PHP extension.

    As I stated at the beginning, this is only a brief guide on what you can do with the DOM API when working with XML documents. If you’re looking for a full reference guide on this library, the best place to go is the official PHP website.

    See you in the next PHP web development tutorial!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · In this last chapter of the series, you’ll learn how to use the DOM XML PHP...
     

       

    PHP ARTICLES

    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5
    - Working with Multiple Document Nodes with th...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway