Home arrow PHP arrow Page 4 - Working with Attributes and CDATA Sections with the DOM XML Extension

Inserting CDATA nodes into an XML document - PHP

Undeniably, one of the most remarkable strengths of PHP is its robust set of extensions. They provide developers with the right tools to build a huge variety of web applications with minor hassles. And this characteristic is particularly evident when it comes to working with XML documents, since PHP comes packaged with a bunch of powerful libraries that allow it to handle XML data in all sorts of clever ways.

  1. Working with Attributes and CDATA Sections with the DOM XML Extension
  2. Reviewing some methods of the DOM XML extension
  3. Working with the createAttribute() method
  4. Inserting CDATA nodes into an XML document
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
March 04, 2008

print this article



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


// insert the root element into the document


// insert additional elements into the document

foreach($elements as $key=>$value){


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

// append cnode section to element


// append element to document



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


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



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!

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: