If, instead of getting rid of your new cd, you decided to trade it with a friend for one that you liked better, you can use the replaceChild method to substitute one node for another.
The replaceChild method takes two parameters - the old node and the new node:
//build new node
$tradedCD =& $cdCollection->createElement("cd");
$tradedCD->attributes["id"] = 4;
$nameNode =& $cdCollection->createElement("name");
$titleNode =& $cdCollection->createElement("title");
$tradedCD->appendChild($nameNode);
$tradedCD->appendChild($titleNode);
$nameNode->appendChild($cdCollection->createTextNode("John Scofield"));
$titleNode->appendChild($cdCollection->createTextNode("Uberjam"));
//perform replacement
$cdLibrary =& $cdCollection->documentElement;
$cdLibrary->replaceChild($cdLibrary->lastChild, $tradedCD);
The replaceChild method returns a reference to the new node.
|