PHP
  Home arrow PHP arrow Page 2 - 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 - Reviewing how to insert attributes and CDATA nodes with the DOM XML library
    ( Page 2 of 4 )

    If you didn’t have the chance to read the previous tutorial of the series, where I explained in detail how to insert attributes and CDATA nodes into an existing XML document, here you have the opportunity to learn these two topics in one single round. All that you need to do is examine the following pair of hands-on examples. The first one demonstrates a simple implementation of the “createAttribute()” method, which is bundled with the DOM XML extension, and the last one shows how to append a few CDATA sections to a given XML string.

    Here are the corresponding code samples:

    // example on creating a new DOMDocument object and appending an attribute using the 'createAttribute()' 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);

    // create attribute

    $attribute=$dom->createAttribute('customattribute');

    // append attribute to element

    $element->appendChild($attribute);

    // 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 customattribute="">Element 1</element1>

    <element2 customattribute="">Element 2</element2>

    <element3 customattribute="">Element 3</element3>

    <element4 customattribute="">Element 4</element4>

    <element5 customattribute="">Element 5</element5>

    <element6 customattribute="">Element 6</element6>

    <element7 customattribute="">Element 7</element7>

    <element8 customattribute="">Element 8</element8>

    <element9 customattribute="">Element 9</element9>

    <element10 customattribute="">Element 10</element10>

    </rootnode>

    */


    // example on creating a new DOMDocument object and appending a new cdata node using the 'createCDATASection()' 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);

    $cdataNode=$dom->createCDATASection(' This is a sample cdata node ');

    // append cnode section to element

    $element->appendChild($cdataNode);

    // 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<![CDATA[ This is a sample cdata node ]]></element1>

    <element2>Element 2<![CDATA[ This is a sample cdata node ]]></element2>

    <element3>Element 3<![CDATA[ This is a sample cdata node ]]></element3>

    <element4>Element 4<![CDATA[ This is a sample cdata node ]]></element4>

    <element5>Element 5<![CDATA[ This is a sample cdata node ]]></element5>

    <element6>Element 6<![CDATA[ This is a sample cdata node ]]></element6>

    <element7>Element 7<![CDATA[ This is a sample cdata node ]]></element7>

    <element8>Element 8<![CDATA[ This is a sample cdata node ]]></element8>

    <element9>Element 9<![CDATA[ This is a sample cdata node ]]></element9>

    <element10>Element 10<![CDATA[ This is a sample cdata node ]]></element10>

    </rootnode>

    */


    Thanks to the two previous examples, you can learn quickly how to use the “createAttribute()” and “createCDATASection()” methods of the DOM XML library to append some basic attributes and CDATA nodes to a determined XML document without needing to scratch your head.

    As you can see, in both cases I complemented the use of the aforementioned methods with “appendChild()” to insert the new nodes into the document tree. Pretty easy to grasp, isn’t it?

    Well, now that you've recalled how to use the “createAttribute()” and “createCDATASection()” methods that come packaged with the DOM XML PHP library, it’s time learn more about the capabilities of this XML-related PHP extension.

    In the section to come, I’m going to show you how to add comment nodes to a given XML document by using the DOM API. Thus, to learn the details of how this process will be performed, please click on the link below and keep 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 4 Hosted by Hostway
    Stay green...Green IT