Inserting Comments and Accessing Nodes with the DOM XML Extension in PHP 5

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.

When it comes to working with XML documents, PHP really shines. It provides web developers with a bunch of thorough extensions aimed at facilitating the manipulation of XML data in all sorts of clever ways. This includes the always popular “simpleXML” library and one that actually is the primary subject of this series of articles. In this case, I’m talking about the DOM XML extension, which allows you to handle XML documents by using the DOM API. Thus, if you’re interested in learning how to put this extension to work for you quickly, look no further, because you’ve come to the right place.

Now that I’ve introduced you to the main subject of this series, I think it’s time to rehash some of the most relevant topics that were treated in the preceding article of the series. Well, as you’ll possibly recall, in the last tutorial I explained how to utilize the “createAttribute()” and “createCDATASection()” methods, which are bundled with the DOM XML extension, to insert attributes and CDATA nodes into an existing XML document.

Since these methods offer an easy learning curve, I assume that you shouldn’t have major difficulties understanding how they can be used to aggregate certain attributes and create CDATA sections within a string of XML data.

The DOM XML library has many other useful methods, aside from the ones I’ve already mentioned, which can be utilized to insert different types of nodes into a given XML document. What’s more, the extension allows you to access specific elements within the document tree by using their corresponding ID attributes. Therefore, in this third installment of the series, I’ll be teaching you specifically how to append comment nodes to a given XML string and how to extract certain elements via their IDs.

Having already outlined the subject of this tutorial, let’s learn how to perform the aforementioned tasks with the API provided by the DOM XML extension. Let’s get going!

{mospagebreak title=Reviewing how to insert attributes and CDATA nodes with the DOM XML library}

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.

{mospagebreak title=Appending comment nodes to a given XML document}

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

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

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

// append comment

$element->appendChild($comment);

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

</rootnode>

*/


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.

{mospagebreak title=Accessing XML nodes by their ID attribute}

If you’re anything like me and spend some time working with JavaScript and the DOM, then it’s highly probable that you’ve already used its “getElementById()” method to access specific elements of a web document.

In this particular case, the DOM XML extension also provides PHP developers with a similar method that obviously performs the same task as its client-side counterpart. However, before using this method to access a particular node within an XML document, not only must the element in question have a value for its ID attribute, but this attribute must be of type “ID.”

Does that sound a bit confusing? It’s not, actually. Nonetheless, to dissipate any possible doubts about how this method works, below I coded a brand new hands-on example that shows how to access a specific node of an XML document using its ID attribute. Here it is:

// example on creating a new DOMDocument object and using the ‘getElementById()’ to extract a node with a specified ID


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

// append attribute to element

$element->setAttribute(‘id’,strtolower($value));

// set attribute of type ID

$element->setIdAttribute(‘id’,true);

// append element to document

$rootElement->appendChild($element);

}

echo ‘The node that contains the id ‘element 3′ is the following: ‘.$dom->getElementById(‘element 3′)->tagName;


/* displays the following

The node that contains the id ‘element 3′ is the following: element3

*/


As you can see in the previous example, an attribute of type ID is assigned to each child node of the pertinent XML document via a brand new method, called “setIdAttribute().” After this operation has been completed successfully, a specific element is accessed via the “getElementById()” method.

In conclusion, you must bear in mind that before using the “getElemementById()” method directly to get access to a specified XML node, it must contain a valid attribute of type ID. Will you be capable of remembering this basic condition? I bet you will!

And finally, you’re free to use all of the code samples included in this tutorial to expand your background in using the DOM XML extension provided by PHP.

Final thoughts

In this third article of the series, you learned how to use two brand new methods packaged with the DOM XML extension (“createComment()” and “getElementById()”) to insert comments into an XML document and also access specific nodes within the document in question.

In the upcoming article of the series, I’ll teach you how to get access to a group of XML nodes via another method that’s used frequently with JavaScript and the DOM. Yes, you guessed right. I’m speaking of the popular “getElementsByTagName()” method. So now that you’ve been warned, you won’t want to miss it!

[gp-comments width="770" linklove="off" ]

antalya escort bayan antalya escort bayan Antalya escort diyarbakir escort