210 CHAPTER 6 DOCUMENT OBJECT MODEL (DOM)
210 CHAPTER 6 DOCUMENT OBJECT MODEL (DOM) /* Create year element */ $year = $dom->createElement(”year”); /* Append text node to set content */ $year->appendChild($yeartxt); $copyright->appendChild($year); After creating the year element, the DOMText object, previously created, is appended as content. Once this is done, the year element is appended to the copyright element. For example: /* Append a newly created holder element with content “Rob Richards” */ $copyright->appendChild(new DOMElement(”holder”, “Rob Richards”)); Again, a single line of code performs multiple operations. A new DOMElementobject is created with the name holderand the value Rob Richards. This element is appended to the copyright element. Manipulating Text The DOMText class derives from the DOMCharacterData class. Methods exist in both classes that can manipulate text on DOMTextobjects. For example, take the following piece of code, which includes the appropriate output that will print after the colon in each of the comments: /* If content is not whitespace then … */ if (! $yeartxt->isElementContentWhitespace()) { /* Print substring at offset 1 and length 2: 00 */ print $yeartxt->substringData(1,2).”n”; /* Append the string -2006 to the content and print output: 2005-2006 */ $yeartxt->appendData(”-2006″); print $yeartxt->nodeValue.”n”; /* Delete content at offset 4 with length of 5 and print output: 2005 */ $yeartxt->deleteData(4,5); print $yeartxt->nodeValue.”n”; /* Insert string “ABC” at offset 1 and print output: 2ABC005 */ $yeartxt->insertData(1, “ABC”); print $yeartxt->nodeValue.”n”; /* Replace content at ofset 1 with length of 3 with an empty string: 2005 */ $yeartxt->replaceData(1, 3, “”); print $yeartxt->nodeValue.”n”; } At this point the tree is really starting to take shape. The output at this point using formatting, of course looks like this:
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Clan Web Hosting services