PHP
  Home arrow PHP arrow Page 4 - Inserting Comments and Accessing Nodes...
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

Inserting Comments and Accessing Nodes with the DOM XML Extension in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2008-03-11

    Table of Contents:
  • Inserting Comments and Accessing Nodes with the DOM XML Extension in PHP 5
  • Reviewing how to insert attributes and CDATA nodes with the DOM XML library
  • Appending comment nodes to a given XML document
  • Accessing XML nodes by their ID attribute

  • 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

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    Inserting Comments and Accessing Nodes with the DOM XML Extension in PHP 5 - Accessing XML nodes by their ID attribute
    (Page 4 of 4 )

    If you’re anything like me and spend some time working with JavaScript and the DOM, then it’s highly probable that you’ve already used its “getElementById()” method to access specific elements of a web document.

    In this particular case, the DOM XML extension also provides PHP developers with a similar method that obviously performs the same task as its client-side counterpart. However, before using this method to access a particular node within an XML document, not only must the element in question have a value for its ID attribute, but this attribute must be of type “ID.”

    Does that sound a bit confusing? It’s not, actually. Nonetheless, to dissipate any possible doubts about how this method works, below I coded a brand new hands-on example that shows how to access a specific node of an XML document using its ID attribute. Here it is:

    // example on creating a new DOMDocument object and using the 'getElementById()' to extract a node with a specified ID


    $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($key,$value);

    // append attribute to element

    $element->setAttribute('id',strtolower($value));

    // set attribute of type ID

    $element->setIdAttribute('id',true);

    // append element to document

    $rootElement->appendChild($element);

    }

    echo 'The node that contains the id 'element 3' is the following: '.$dom->getElementById('element 3')->tagName;


    /* displays the following

    The node that contains the id 'element 3' is the following: element3

    */


    As you can see in the previous example, an attribute of type ID is assigned to each child node of the pertinent XML document via a brand new method, called “setIdAttribute().” After this operation has been completed successfully, a specific element is accessed via the “getElementById()” method.

    In conclusion, you must bear in mind that before using the “getElemementById()” method directly to get access to a specified XML node, it must contain a valid attribute of type ID. Will you be capable of remembering this basic condition? I bet you will!

    And finally, you’re free to use all of the code samples included in this tutorial to expand your background in using the DOM XML extension provided by PHP.

    Final thoughts

    In this third article of the series, you learned how to use two brand new methods packaged with the DOM XML extension (“createComment()” and “getElementById()”) to insert comments into an XML document and also access specific nodes within the document in question.

    In the upcoming article of the series, I’ll teach you how to get access to a group of XML nodes via another method that’s used frequently with JavaScript and the DOM. Yes, you guessed right. I’m speaking of the popular “getElementsByTagName()” method. So now that you’ve been warned, you won’t want to miss it!


    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.

       · Over the course of this third installment of the series, you’ll see how to use the...
     

       

    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 6 hosted by Hostway