PHP
  Home arrow PHP arrow Page 4 - Working with Attributes and CDATA Sect...
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

Working with Attributes and CDATA Sections with the DOM XML Extension
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-03-04

    Table of Contents:
  • Working with Attributes and CDATA Sections with the DOM XML Extension
  • Reviewing some methods of the DOM XML extension
  • Working with the createAttribute() method
  • Inserting CDATA nodes into an XML document

  • 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

    Virtual Tradeshows by Ziff Davis Enterprise - A Unique Opportunity to Connect with IT Experts, Access Information, and Gain Insight on today's Technology

    Working with Attributes and CDATA Sections with the DOM XML Extension - Inserting CDATA nodes into an XML document
    (Page 4 of 4 )

    The DOM XML extension provides PHP developers with the capacity to add CDATA nodes to an existing XML document via another handy method, called “createCDATASection().” As you learned earlier when working with attributes, CDATA sections must be explicitly included into the document in question via the “appendChild()” method. Otherwise, they’ll remain in memory forever and won’t be displayed at all.

    Having explained how the “createCDATASection()” method functions, have a look at the example below, which demonstrates how to append these types of nodes to a simple XML string.

    The corresponding code sample is as follows:


    // 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>

    */


    See how easy it is to create and append a few simple CDATA nodes to an XML string using the “createCDATASection()” method? No head scratching and desperate screams are needed in this case (in others, this could be a viable solution, definitely), since the DOM XML extension simplifies this process.

    As usual with many of my articles on PHP web development, my final recommendation is that you practice intensively with all the code samples shown in this tutorial to improve your skills.

    Final thoughts

    In this second installment of the series I walked you through appending new attributes and CDATA nodes to an XML document by using the handful of intuitive methods that come bundled with the DOM XML PHP extension.

    In the next chapter of this journey, I’ll show you how to insert comments into an XML string, as well as how to access specific nodes by their ID attribute, so you don’t have any excuses 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.

       · The DOM XML PHP library comes equipped with a bunch of methods that can be useful...
     

       

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