Home arrow PHP arrow Page 3 - Inserting Comments and Accessing Nodes with the DOM XML Extension in PHP 5

Appending comment nodes to a given XML document - PHP

Welcome to the third chapter of the series “A quick overview of the DOM XML extension in PHP 5.” As the article’s title suggests, this series walks you through using the most relevant methods that come packaged with this powerful XML-related PHP extension to perform specific operations on XML documents by using the API of the Document Object Model.

  1. Inserting Comments and Accessing Nodes with the DOM XML Extension in PHP 5
  2. Reviewing how to insert attributes and CDATA nodes with the DOM XML library
  3. Appending comment nodes to a given XML document
  4. Accessing XML nodes by their ID attribute
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
March 11, 2008

print this article



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


// insert the root element into the document


// insert additional elements into the document

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


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

// append comment


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



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

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: