Cheap Web Hosting for Developers

PHP, MySQL, Java, Unix Cheap Web Hosting

212 CHAPTER 6 DOCUMENT OBJECT MODEL (DOM)

Filed under: PHP and XML — webmaster @ 07:11

212 CHAPTER 6 DOCUMENT OBJECT MODEL (DOM) /* Create DOMEntityReference */ $entityref = $dom->createEntityReference(”lt”); $entityref = new DOMEntityReference(”lt”); /* Results in < */ Outside the methods inherited from the DOMNode class, the DOMDocumentFragment class is the only class with additional functionality. This functionality is only a single method and available only in PHP 5.1 and higher. Rather than having to build a fragment manually by appending nodes, you can use the method appendXML() to create a fragment from string data. Take the case of building a fragment manually versus building it from a string: $frag = $dom->createDocumentFragment(); $frag->appendChild(new DOMElement(”node1″, “node1 value”)); $frag->appendChild(new DOMElement(”node2″, “node2 value”)); It would have been so much easier to append the data as a string. You had no need to manually create the DOMElement objects because the appropriate nodes are automatically created through the appendXML() method: $frag = $dom->createDocumentFragment(); $frag->appendXML(”node1 valuenode2 value“); Note When appending a DOMDocumentFragment object into a tree, only the children on the fragment are added. The DOMDocumentFragment object that is left after an append will be empty because the nodes have been removed and inserted into the tree. Removing and Replacing Nodes The last piece of editing a document is removing and replacing nodes in a tree. Some of the methods encountered so far will perform this type of functionality. Take, for instance, the setAttributeNode() method. When a node with the same name exists on the element, the old attribute is removed and replaced with the new attribute node, and the old attribute is returned. The same functionality can happen with other node types using the replaceChild()method. Sometimes, however, you want just to remove a node. In this case, you can use the removeChild() method. Given the following document loaded into a DOMDocument object: $doc = DOMDocument::loadXML(’ child1 content child2 content child3 content ‘); the element child2 needs to be removed from this document, and child3 needs to be replaced with the element newchild. The first step is to get access to each of these nodes. To reduce the

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