PHP
  Home arrow PHP arrow Page 3 - 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 - Appending comment nodes to a given XML document
    ( Page 3 of 4 )

    Whether you’re using a specific server-side scripting language, like PHP, or simply structural markup, you know that comments are necessary; they make your code more readable and easier to comprehend. In this case, the DOM XML extension is no exception. It also offers a method, called “createComments()”, which obviously comes in handy when inserting comments into a determined XML document.

    As for attributes and CDATA nodes, appending comments to an XML string requires using the “appendChild()” method that you saw in previous examples. The method is used to insert the comments directly into the document tree.

    Now that I have explained how this method works, please take a look at the following example, which illustrates how to use it with a sample XML document. Here is the pertinent sample code:


    // example on creating a new DOMDocument object and appending a new comment node using the 'createComment()' method


    $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);

    $comment=$dom->createComment('This is a sample comment');

    // append comment

    $element->appendChild($comment);

    // append element to document

    $rootElement->appendChild($element);

    }

    // tell the browser the output is XML via the 'Content-Type' HTTP header

    header('Content-Type: text/xml');

    // display DOM document

    echo $dom->saveXML();


    /* displays the following

    <?xml version="1.0" encoding="iso-8859-1"?>

    <rootnode>

    <element1>Element 1<!--This is a sample comment--></element1>

    <element2>Element 2<!--This is a sample comment--></element2>

    <element3>Element 3<!--This is a sample comment--></element3>

    <element4>Element 4<!--This is a sample comment--></element4>

    <element5>Element 5<!--This is a sample comment--></element5>

    <element6>Element 6<!--This is a sample comment--></element6>

    <element7>Element 7<!--This is a sample comment--></element7>

    <element8>Element 8<!--This is a sample comment--></element8>

    <element9>Element 9<!--This is a sample comment--></element9>

    <element10>Element 10<!--This is a sample comment--></element10>

    </rootnode>

    */


    As you can see in the above example, a few basic comment nodes have been appended to a basic XML document by using the “createComment()” method. In this case, I used a trivial string to construct the comments in question, but you may want to change the signature of the previous PHP script and introduce more meaningful remarks.

    So far, everything looks fine, since at this stage, I showed you how to incorporate some simple comments to an existing XML document. This can be really useful if you wish to keep your code easy to read and maintain. Therefore, assuming that you've grasped the logic required for this process, the next topic that I’m going to cover in this article will be how to access a particular node within a given XML string via its ID attribute.

    To see how this will be achieved, please read the next few lines. They’re only one click away.



     
     
    >>> 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 1 hosted by Hostway
    Stay green...Green IT