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.
blog comments powered by Disqus |
|
|
|
|
|
|
|