Working with Attributes and CDATA Sections with the DOM XML Extension

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.

A good example of this is the popular “SimpleXML” extension, which comes in handy when performing the most common operation required to work with XML documents — using an approachable, easy-to-learn API. You might think that this library is the only one bundled with PHP that can be used to handle XML data, but this is completely erroneous.

There’s a PHP library that permits you to work with XML documents by using the DOM API. Does this name ring any bells for you? Yes, you’re right! Fortunately, PHP also provides programmers with the helpful DOM XML extension, which, as its name suggests, can be quite useful when processing XML documents via the Document Object Model.

Basically, this implies that you’ll be able to build XML documents from scratch, create and append new nodes, remove existing ones, work with custom attributes, etc., by using the methods offered by the DOM API, which include the popular “createElement(),” appendChild(),” and “getElementById().”

Of course, if you’re used to working with JavaScript on a frequent basis, then the previous methods will be pretty familiar to you. But if you haven’t had the chance to develop client-side applications, you might want to read this article series to learn how to get started using the DOM XML extension bundled with PHP.

And speaking of the numerous features offered by this library, in the first article of the series I explained how to use some of its simplest methods to perform certain basic tasks on XML data, such as building new documents, creating elements and inserting nodes, and finally echoing the whole output to a browser.

However, this is merely a humble beginning, since the DOM XML library comes equipped with many other methods that can be useful for adding custom attributes to the existing nodes of a given XML document, and creating new CDATA sections, among other things. Therefore, assuming that all these topics are really interesting to you, in this second part of the series I’ll be discussing them in detail and logically accompanying the corresponding theoretical concepts with illustrative hands-on examples.

Does all this material sound good to you? Great! Let’s continue this journey now!

{mospagebreak title=Reviewing some methods of the DOM XML extension}

Before I begin showing you how to use the DOM XML extension that comes packaged with PHP, first I will rehash the methods that were covered in the previous installment of the series, in case you weren’t able to read it.

In that specific tutorial, I introduced a few basic methods, such as “createElement()”and “appendChild(),” which were pretty useful for building and inserting new nodes into an existing XML document. I culminated the article by developing a complete practical example where all these methods, along with an additional one called “saveXML(),” were utilized to build a new XML document populated by using a basic PHP array. The source code corresponding to this example is listed below. Here it is:


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

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

<element2>Element 2</element2>

<element3>Element 3</element3>

<element4>Element 4</element4>

<element5>Element 5</element5>

<element6>Element 6</element6>

<element7>Element 7</element7>

<element8>Element 8</element8>

<element9>Element 9</element9>

<element10>Element 10</element10>

</rootnode>

*/


As you can see, the above example speaks for itself, due to its extreme simplicity. In this case, a simple PHP array is used to create and append a few new nodes to an XML document via the pair of “createElement()” and “appendChild()” methods I mentioned above. Once those nodes are appended to the document tree, the whole structure is printed on screen through the “saveXML()” method. That wasn’t too difficult to grasp, was it?

With the previous example still fresh in your mind, I assume that using the DOM XML PHP extension to build a basic XML document from scratch and add new nodes to it is already a familiar process for you. This means that it’s time to continue learning more useful methods that come integrated with this extension.

In the following section, I’m going to teach you how to add some custom attributes to the existing nodes of a recently-created XML document and how to insert CDATA sections into it. All of these topics will be treated in depth in the next few lines, so click on the link that appears below and keep reading.

{mospagebreak title=Working with the createAttribute() method}

Undoubtedly, one of the most common operations involved in the manipulation of XML documents is the creation of custom attributes that can be appended to one or more nodes. To perform this specific task, the DOM XML extension provides an intuitive method called “createAttribute(),” which can be used to dynamically build new attributes.

Before I show you a concrete example of how to use this specific method, you should be aware that once a new attribute has been created, it must be inserted into the pertinent XML document via the “appendChild()” method (or a similar one). As you’ll see in a moment, all of the attributes should be appended to the document tree, as it is done with regular nodes.

Having clarified this point, pay attention to the following example, which shows how to use the method in question to assign a few simple attributes to several nodes of an existing XML document.


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

*/


As shown by the previous example, adding a custom attribute to different nodes of a given XML document is actually a no-brainer process that can be performed with minor effort. In this case, I used the “createAttribute()” method to construct the attribute in memory, and then insert it into the document tree via the pertinent “appendChild()” method.

Lastly, the recently-created document is outputted to the browser through the “saveXML()” method that I introduced in the preceding article of this series. It’s worthwhile to mention two things here: first, no specific values have been assigned to the respective attributes, and second, all of the child nodes in the above XML document are dynamically generated from a basic PHP array.

Of course, the structure of the previous XML document is rather incomplete, since its attributes don’t contain a specific value. However, don’t worry about this issue for the time being, since in upcoming articles of this series, I’ll be discussing how to do this in detail.

So far, so good, right? At this time, you’ve hopefully learned how to insert some custom attributes into an existing XML document by using the API of the DOM XML extension. So, where do we go from here? Good question. In the last section of this article, I’ll teach you how to insert CDATA sections into a given XML string.

However, to see how this will be done, you’ll have to read the next few lines. Just click on the link below.

{mospagebreak title=Inserting CDATA nodes into an XML document}

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!

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

chat sex hikayeleri Ensest hikaye