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

Inserting Comments and Accessing Nodes with the DOM XML Extension in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    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:
      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


    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!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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