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

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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