CHAPTER 6 DOCUMENT OBJECT MODEL (DOM) 209
CHAPTER 6 DOCUMENT OBJECT MODEL (DOM) 209 are the counterparts to the getAttribute() and getAttributeNS() methods you encountered earlier when navigating the tree. Both of the set methods create an attribute based on the name and value, passed as parameters, and return the newly created DOMAttr object. Just like all the other namespace functions, getAttributeNS() accepts a namespace URI as a parameter and uses a qualified name as an argument: /* Equivalent calls to create the lang attribute with value “en” */ $bookinfo->setAttribute(”lang”, “en”); $bookinfo->setAttributeNS(NULL, “lang”, “en”); Caution When creating an attribute with an entity reference as a value, you must create a DOMAttr object and set the value manually. The value argument for the constructor of a DOMAttr and for the setAttribute() and setAttributeNS() methods is simple text that is not parsed and treated as literal text. Text Nodes Text nodes are simple nodes, because they cannot have child nodes or attributes. In other words, they simply contain text content. This does not mean they offer little functionality, though. You can use the text nodes to set content as well as perform string functions. You create and insert them in the same manner as element nodes. You can create them either using a factory method from a DOMDocument object or using the new keyword. You can insert them using the normal appendChild() and insertBefore()methods. Creating and Inserting Text Nodes You use a DOMDocument object to create a text node with the createTextNode() method. A data parameter is required that specifies the content, or value, for the text node. Instantiating a DOMText object with the new keyword does not require a value, because the default is to create a text node with empty content. For example: /* Equivalent creation of DOMText objects */ $yeartxt = $dom->createTextNode(”2005″); $yeartxt = new DOMText(”2005″); The text node created, whichever method you decide to use, will be used as the content for the yet-to-be-created yearelement, which will be the child of a yet-to-be-created copyright element. While inserting these nodes, this also creates the holder element. For example: /* Create and Append a copyright element */ $copyright = $bookinfo->appendChild(new DOMElement(”copyright”)); In one line, a new copyright element is instantiated using the new keyword and is appended to the bookinfo element. You might have wondered why the return values mattered before because all examples previously used instantiated objects when appending nodes. In this case, the $copyright variable, upon the method returning, will contain the newly created DOMElement object that contains the copyright element. For example:
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Clan Web Hosting services