Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : DOM Functions : DOMNode->cloneNode()

DOMNode->cloneNode()

Clones a node ()


Code Examples / Notes » dom_domnode_clonenode

oliver dot christen

simple exemple of node cloning
<?xml version="1.0"?>
<book type="paperback">
<title name='MAP'>Red Nails</title>
<price>$12.99</price>
<author>
<name first="Robert" middle="E" last="Howard"/>
<birthdate>9/21/1977</birthdate>
</author>
<author>
<name first="Arthur" middle="Mc" last="Kayn"/>
</author>
</book>
<?php
//filename xml file to use
$file = 'book.xml';
$doc = new domDocument;
if (file_exists($file)) {
$doc->load($file);
} else {
exit('Erreur !.');
}
$xpath = new domXPath($doc);
$query = "//author/*";
$xpathQuery = $xpath->query($query);
$size = $xpathQuery->length;
for ($i=0; $i<$size; $i++){
$node = $xpathQuery->item($i);
if ($node->nodeName == 'birthdate' && $node->hasChildNodes() && $node->firstChild->textContent != ''){
$clonenode = $node->cloneNode(true);
$refnode = $node;
}
}
for ($i=0; $i<$size; $i++){
$node = $xpathQuery->item($i);
if (!$node->isSameNode($refnode)){
$newnode = $node->appendChild($clonenode);
}
}
print $doc->saveXML();
?>


dtorop932

For those converting from PHP 4 DOM XML to PHP 5 DOM extension:
In the old days, DOM XML's clone_node() was the way to copy nodes from one DOM to another, as well as to perform intra-DOM copies.  Nowadays, DOM's cloneNode() is the intra-DOM solution, but see importNode() (http://php.net/dom-domdocument-importnode) to copy nodes from one DOM to another.
So to append root node of $dom2 and all its children into root node of $dom1:
PHP 4 (DOM XML):
<?
$root1 = $dom1->document_element();
$other_node = $dom2->document_element();
$root1->append_child($other_node->clone_node(true));
?>
PHP 5 (DOM):
<?
$dom1->documentElement->appendChild(
 $dom1->importNode($dom2->documentElement, true));
?>


Change Language


Follow Navioo On Twitter
DOMAttr->__construct()
DOMAttr->isId()
DOMCharacterData->appendData()
DOMCharacterData->deleteData()
DOMCharacterData->insertData()
DOMCharacterData->replaceData()
DOMCharacterData->substringData()
DOMComment->__construct()
DOMDocument->__construct()
DOMDocument->createAttribute()
DOMDocument->createAttributeNS()
DOMDocument->createCDATASection()
DOMDocument->createComment()
DOMDocument->createDocumentFragment()
DOMDocument->createElement()
DOMDocument->createElementNS()
DOMDocument->createEntityReference()
DOMDocument->createProcessingInstruction()
DOMDocument->createTextNode()
DOMDocument->getElementById()
DOMDocument->getElementsByTagName()
DOMDocument->getElementsByTagNameNS()
DOMDocument->importNode()
DOMDocument->load()
DOMDocument->loadHTML()
DOMDocument->loadHTMLFile()
DOMDocument->loadXML()
DOMDocument->normalizeDocument()
DOMDocument->registerNodeClass()
DOMDocument->relaxNGValidate()
DOMDocument->relaxNGValidateSource()
DOMDocument->save()
DOMDocument->saveHTML()
DOMDocument->saveHTMLFile()
DOMDocument->saveXML()
DOMDocument->schemaValidate()
DOMDocument->schemaValidateSource()
DOMDocument->validate()
DOMDocument->xinclude()
DOMDocumentFragment->appendXML()
DOMElement->__construct()
DOMElement->getAttribute()
DOMElement->getAttributeNode()
DOMElement->getAttributeNodeNS()
DOMElement->getAttributeNS()
DOMElement->getElementsByTagName()
DOMElement->getElementsByTagNameNS()
DOMElement->hasAttribute()
DOMElement->hasAttributeNS()
DOMElement->removeAttribute()
DOMElement->removeAttributeNode()
DOMElement->removeAttributeNS()
DOMElement->setAttribute()
DOMElement->setAttributeNode()
DOMElement->setAttributeNodeNS()
DOMElement->setAttributeNS()
DOMElement->setIdAttribute()
DOMElement->setIdAttributeNode()
DOMElement->setIdAttributeNS()
DOMEntityReference->__construct()
DOMImplementation->__construct()
DOMImplementation->createDocument()
DOMImplementation->createDocumentType()
DOMImplementation->hasFeature()
DOMNamedNodeMap->getNamedItem()
DOMNamedNodeMap->getNamedItemNS()
DOMNamedNodeMap->item()
DOMNode->appendChild()
DOMNode->cloneNode()
DOMNode->hasAttributes()
DOMNode->hasChildNodes()
DOMNode->insertBefore()
DOMNode->isDefaultNamespace()
DOMNode->isSameNode()
DOMNode->isSupported()
DOMNode->lookupNamespaceURI()
DOMNode->lookupPrefix()
DOMNode->normalize()
DOMNode->removeChild()
DOMNode->replaceChild()
DOMNodelist->item()
DOMProcessingInstruction->__construct()
DOMText->__construct()
DOMText->isWhitespaceInElementContent()
DOMText->splitText()
DOMXPath->__construct()
DOMXPath->evaluate()
DOMXPath->query()
DOMXPath->registerNamespace()
dom_import_simplexml
eXTReMe Tracker