PHP
  Home arrow PHP arrow Page 2 - Working with Multiple Document Nodes w...
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 
 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

Working with Multiple Document 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 / 2
    2008-03-18

    Table of Contents:
  • Working with Multiple Document Nodes with the DOM XML Extension in PHP 5
  • Getting a collection of XML nodes
  • Loading XML data from a specified text file with the load() method
  • Reading data from a specified XML string with the loadXML() 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Working with Multiple Document Nodes with the DOM XML Extension in PHP 5 - Getting a collection of XML nodes
    (Page 2 of 4 )

    During the course of the prior tutorial of the series, you saw how to use the “getElementById()” method included in DOM XML extension to access specific nodes within an XML document by its ID attribute. In a similar manner, the extension also provides the “getElementsByTagName()” method, which as its name suggests, can be used to retrieve a collection of nodes that share the same tag name.

    If you ever used the client-side version of this method with JavaScript, then surely you’ll find it very easy to learn and use. Still, it's better that I show you a concrete example of how to utilize it, so you can quickly grasp how it works.

    Please take a look at the following code sample, aimed at demonstrating the functionality of the aforementioned “getElementsByTagName()” method:

    // example on creating a new DOMDocument object and using the 'getElementsByTagName()' method to extract nodes with a specific tag name

    $elements=array('element1'=>'Element 1','element2'=>'Element 2','element3'=>'Element 3','element4'=>'Element 4','element5'=>'Element 5','element6'=>'Element 6','element7'=>'Element 7','element8'=>'Element 8','element9'=>'Element 9','element10'=>'Element 10');

    $dom=new DOMDocument('1.0','iso-8859-1');

    $rootElement=$dom->createElement('rootnode','');

    // insert the root element into the document

    $dom->appendChild($rootElement);

    // insert additional elements into the document

    foreach($elements as $key=>$value){

    $element=$dom->createElement('testnode',$value);

    // append element to document

    $rootElement->appendChild($element);

    }

    // display nodes with 'testnode' name

    foreach($dom->getElementsByTagName('testnode') as $node){

    echo $node->tagName.'<br />';

    }


    /* displays the following

    testnode

    testnode

    testnode

    testnode

    testnode

    testnode

    testnode

    testnode

    testnode

    testnode

    */


    As you can see, the above PHP 5 script first creates a simple XML document by using a PHP array, and then inserts a few basic nodes into it. Finally, it displays the entire contents on the browser. So far, nothing unexpected happens, right? However, notice how in the end, the pertinent “getElementsByTagName()” method is used to extract all the nodes that have a <testnode> tag name, and finally iterate over all of them.

    In this case, it’s clear to see the similarity between the previous method and the one used to access multiple elements of a web document via the DOM. Of course, here I’m using the “getElementsByTagName()” method to retrieve multiple nodes within an XML document in the server, but surely you’ll agree that this process is nearly identical to its client-side version.

    All right, at this point you learned how to get collections of XML nodes that have the same tag name, thanks to a simple example. The question is, what’s the next method of the DOM XML extension that needs to be discussed in detail? Well, since in the introduction of this article I promised to teach you how to process multiple XML nodes, in the following section, I’ll be examining a brand new method. It's called “load()” and it will be useful for loading XML data from a remote file.

    To see how the method in question works, click on the link below and start reading.

    More PHP Articles
    More By Alejandro Gervasio


       · In this fourth tutorial of the series, you’ll learn how to get access to multiple...
       · I'm finding this collection of articles extremely long winded. Every article...
       · Thank you for commenting on my PHP article. Sorry to hear you didn’t like it,...
     

       

    PHP ARTICLES

    - Setting Up a Web-based Image Hosting Service
    - 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

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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