PHP
  Home arrow PHP arrow Page 2 - 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 
IBM Developerworks
 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 - 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


       · 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...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway