Browse DevX
Sign up for e-mail newsletters from DevX


Accessing and Manipulating the DOM with PHP : Page 3

Discover how to create a DOM tree from scratch or modify an existing DOM tree using PHP 5.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Cloning a Node

Cloning a node means creating a new node of the same type and (optionally) with the same content of a current node. You can clone nodes using the cloneNode method:

DOMNode DOMNode::cloneNode([ bool $deep]): Creates a clone of the current node; the $deep argument specifies whether to also copy descendants of the current node. The default value is FALSE. For example, the following code clones the <author> element and appends it as a child of the original <author> element. Figure 4 shows the output:

//Set the reference node $author = $root->getElementsByTagName('author')->item(0); //Call the cloningNode function cloningNode($author); //This function clone the $currentNode function cloningNode($currentNode) { $clonenode = $currentNode -> cloneNode(true); $newnode = $currentNode->appendChild($clonenode); }

Figure 4. Cloning Nodes: Cloning the <author> child node and appending it to the original <author> node results in this output. The doubled text value of the original node occurs because retrieving the text value of a node retrieves its child node text values as well.

Removing Child Nodes

To remove a node from the DOM tree use the removeChild method:

DOMNode DOMNode::removeChild(DOMNode $oldnode): This function removes a child node. The $oldnode argument specifies which child node to remove. As an example, the following code removes the child from the books.xml document. You can see from the results in Figure 5 that the bibliography node is missing:

//Get a reference to the bibliography node $bibliography = $root->getElementsByTagName( 'bibliography')->item(0); //Call the removingChild function removingChild($bibliography); //This function remove the $currentNode node function removingChild($currentNode) { $oldbibliography = $root->removeChild($currentNode); }

Figure 5. Removing Nodes: After removing the last child node (<bibliography>, inserted earlier with the appendChild method), listing the node names and values shows that the node is indeed gone.

Replacing Nodes

To replace an existing node with a new node, use the replaceChild method:

DOMNode DOMNode::replaceChild(DOMNode $newnode, DOMNode $oldnode): This function replaces $oldnode with $newnode child if the new node is not already a child of a different parent.

For example, suppose you want to replace the ISBN child node with a new code child node:

//Get the ISBN node $element = $dom->getElementsByTagName('ISBN')->item(0); //Create the new <code> element $code = $dom->createElement('code', '909090'); //Call the replacingNode function replacingNode($code,$element); //This function replaces $currentNode with $node function replacingNode($currentNode, $node) { $node->parentNode->replaceChild($currentNode, $node); }

The output in Figure 6 shows that the node was replaced.

Figure 6. Replacing Nodes: Here's the relevant portion of the document after replacing the <ISBN> node with the new <code> node.

Thanks for your registration, follow us on our social networks to keep up-to-date