If you’ve ever used the DOM API with JavaScript to dynamically remove several elements of a web page, then you’ll find the “removeChild()” method of the DOM XML extension very easy to grasp, since its functionality is identical to that of the one provided by its client-side counter part. So, to do things a bit easier and faster, I’m going to use the same “headlines.xml” file that you saw earlier to exemplify how the “removeChild()” method works. That being said, here’s the familiar signature of this basic 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> And here’s a simple script that uses the “removeChild()” method to delete the entirety of the <image> elements contained by the parent <headline> nodes. The corresponding code sample looks like this: // example on removing child nodes using the 'removeChild()' method $dom=new DOMDocument; // load XML data from existing file $dom->load('headlines.xml'); // get <headline> nodes $headlines=$dom->getElementsByTagName('headline'); foreach($headlines as $headline){ while($headline->hasChildNodes()){ // remove child node $headline->removeChild($headline->childNodes->item(0)); } } Do you realize how easy it is to delete child nodes from an XML document? I guess you do! As you saw, in the above hands-on example, I used a combination of the “removeChild()” method and the brand new “childNodes” object in order to navigate to the first item of each “<headline> node and delete it. Of course, this is merely a basic demonstration of how to implement the “removeChild()” method in a useful manner. But you’re completely free to develop your own testing examples regarding the use of this one and the rest of the methods discussed in this article. Final thoughts It’s hard to believe, but we’ve come to the end of this series. In general terms, the experience has been instructive and also fun, since you learned how to use some of the most relevant methods that come packaged with the DOM XML PHP extension. As I stated at the beginning, this is only a brief guide on what you can do with the DOM API when working with XML documents. If you’re looking for a full reference guide on this library, the best place to go is the official PHP website. See you in the next PHP web development tutorial!
blog comments powered by Disqus |
|
|
|
|
|
|
|