DOMNode::cloneNode()函数是PHP中的内置函数,用于创建节点的副本。
用法:
DOMNode DOMNode::cloneNode( bool $deep )
参数:该函数接受单个参数$deep,该参数指示是否复制所有后代节点。默认情况下,此参数设置为FALSE。
返回值:此函数返回克隆的节点。
程序1:
// Create a DOMDocument
$doc = new DOMDocument();
// Load XML
$doc->loadXML('');
// Create an heading element on DOMDocument object
$h1 = $doc->createElement('h1', "geeksforgeeks");
// Append the child
$doc->documentElement->appendChild($h1);
// Create a new DOMDocument
$doc_new = new DOMDocument();
// Deep clone the node to new instance
$doc_new = $doc->cloneNode(true);
// Render the cloned instance
echo $doc_new->saveXML();
?>
输出:
程序2:
// Create a DOMDocument
$doc = new DOMDocument('1.0', 'iso-8859-1');
// Load XML
$doc->loadXML('');
// Create an heading element on DOMDocument object
$h1 = $doc->createElement('h1', "geeksforgeeks");
// Append the child
$doc->documentElement->appendChild($h1);
// Shallow clone the node to a new instance
// It will clone only the instance not its
// children nodes
$doc_new = $doc->cloneNode(false);
// Render the cloned instance
echo $doc_new->saveXML();
?>
输出:按Ctrl + U查看DOM