As I promised in the section that you just read, the last method that I’m going to teach you in this article will be one that allows you to clone the nodes of a given XML document. Obviously, this process can be extremely useful when it comes to copying nodes within an XML string. Thus, the DOM XML extension offers its handy “cloneNode()” method to perform all of these tasks. To demonstrate how the “cloneNode()” method works, I’m going to use it in conjunction with the previous “hasAttribute().” In this particular case, it will be used for cloning some nodes from the “headlines.xml” file that you saw earlier. That being explained, here’s the signature of this simple XML file: // definition of 'headlines.xml' file <?xml version="1.0" encoding="iso-8859-1"?> <headlines> <headline id="economics"> <image>image1.jpg</image> <url>Link for headline 1 goes here</url> <text>Text for headline 1 goes here</text> </headline> <headline id="sports"> <image>image2.jpg</image> <url>Link for headline 2 goes here</url> <text>Text for headline 2 goes here</text> </headline> <headline id="jetset"> <image>image3.jpg</image> <url>Link for headline 3 goes here</url> <text>Text for headline 3 goes here</text> </headline> <headline id="technology"> <image>image4.jpg</image> <url>Link for headline 4 goes here</url> <text>Text for headline 4 goes here</text> </headline> <headline id="art"> <image>image5.jpg</image> <url>Link for headline 5 goes here</url> <text>Text for headline 5 goes here</text> </headline> </headlines> Now that I have listed the complete signature of the “headlines.xml” file, please pay attention to the following hands-on example. It first clones the <headlines> nodes of the file, and then displays the respective values of their ID attributes. The corresponding code sample is as follows: // example on using the 'hasAttribute()' and 'cloneNode()' methods altogether $dom=new DOMDocument(); // load XML data from existing file $dom->load('headlines.xml'); $headlines=$dom->getElementsByTagName('headline'); foreach($headlines as $headline){ // clone node $cloneNode=$headline->cloneNode(false); if($cloneNode->hasAttribute('id')){ echo 'ID attribute of current cloned node is the following: '.$cloneNode->getAttribute('id').'<br />'; } } As you can see by the above code sample, I used the intuitive “cloneNode()” method to clone all of the <headline> nodes of the previous sample “headlines.xml” file. In this case, I utilized the “hasAttribute()” method that I showed you in the previous section to determine whether the recently-cloned elements have attributes or not. This provides you with a concrete example of how to clone the nodes of an XML document. Finally, I recommend that you develop your own set of test examples by using all of the code samples included in this tutorial. The purpose is to improve your skills in using the recently explained method. Final thoughts That’s all for now. In this sixth chapter of the series, you hopefully expanded your background on the DOM XML library by learning how to work with attributes and cloned nodes of an XML document. In the next and last article, I’m going to teach you a couple of additional methods that will come in handy when dealing with child nodes. Now that you’ve been warned about the topics that will be discussed in the final tutorial of the series, you won’t want to miss it!
blog comments powered by Disqus |
|
|
|
|
|
|
|