PHP
  Home arrow PHP arrow Page 2 - Working with Multiple Document 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? 
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: starstarstarstarstar / 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:
      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


    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
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT