Cheap Web Hosting for Developers

PHP, MySQL, Java, Unix Cheap Web Hosting

CHAPTER 6 DOCUMENT OBJECT MODEL (DOM) 213

Filed under: PHP and XML — webmaster @ 17:01

CHAPTER 6 DOCUMENT OBJECT MODEL (DOM) 213 number of steps, I will show how to retrieve the elements using the getElementsByTagName() method: $root = $doc->documentElement; $child2 = $root->getElementsByTagName(”child2″)->item(0); $child3 = $root->getElementsByTagName(”child3″)->item(0); The first step is to remove the $child object: $root->removeChild($child2); If you look at the serialized tree now, you would see this: child1 content child3 content The whitespaces are left in the document, causing the blank line in the output. The $child3 object is still in scope so can now be replaced with a new element. This also will be condensed using the new keyword for the new element: $oldchild = $root->replaceChild(new DOMElement(”newchild”, “new content”), $child3); In this case, the new element is being created inline. Unfortunately, using the new keyword here does not give direct access to the newly created node. This method returns the node being removed from the tree. The resulting serialized tree is as follows: child1 content new content Wrapping up this section, you might want to remove those whitespaces within the root element children. I have already covered everything you need to know in order to do this. One way is to use the following piece of code: $children = $root->childNodes; for ($x=$children->length; $x–; $x>=0) { $node = $children->item($x); if ($node->nodeType == XML_TEXT_NODE && $node-> isElementContentWhitespace()) { $root->removeChild($node); } } You have many ways to accomplish this task. One question you may have is why the iteration was performed from last to first. Based on how this code was written, DOMNodeListobjects are being used. These are live collections resulting in changes of indexes when nodes are

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP MySQL Web Hosting services

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Sorry, the comment form is closed at this time.

Powered by Cheap Web Hosting