We can add a "name" and "title" node to the new cd node in the same way as the last step.
//grab a reference to the new cd node that you just created
$newNode =& $cdCollection->documentElement->lastChild;
//add the child elements "name" and "title"
$newNode->appendChild($cdCollection->createElement("name"));
$newNode->appendChild($cdCollection->createElement("title"));
Note that we do not actually populate these nodes with the artist name and album title. This is because textual XML data is not of type Element. It is actually a different category of node altogether, either of type TextNode or CDataSection.
As mentioned earlier, the artist's name of your new album is "W3C". This is textual data that belongs in a TextNode. In DOMIT! this is a node of class DOMIT_TextNode, which is created as such:
$newTextNode =& $cdCollection->createTextNode("W3C");
The new text node is appended to the "name" element in the usual manner:
$newCDNode =& $cdCollection->documentElement->lastChild;
$nameNode =& $newCDNode->firstChild;
$nameNode->appendChild($newTextNode);
setText
The setText method of an element provides a shortcut for creating and populating
a child text node.
When you call the setText method of an element, it will:
- first check if the calling element has a child text node
- if a child text node does exist, it will be populated with the data specified
- if a child text node does not exist, a child text node will be created, then populated with the data specified
For example:
$someElement =& $xmldoc->createElement('myElement');
$someElement->setText('This is the node value of someElements child text node');
|