DOMDocument::importNode
DOMDocument::importNode
(PHP 5, PHP 7)
DOMDocument :: importNode - 将节点导入当前文档
描述
public DOMNode DOMDocument::importNode ( DOMNode $importedNode [, bool $deep ] )
此函数返回要导入的节点的副本并将其与当前文档关联。
参数
importedNode
要导入的节点。
deep
如果设置为TRUE
,则此方法将递归地导入该树下的子树importedNode
。
注意
:复制节点属性deep
需要设置为TRUE
返回值
复制的节点或FALSE
,如果它不能被复制。
错误/异常
如果节点无法导入,则会引发DOMException。
例子
示例#1 DOMDocument :: importNode()示例
在文档之间复制节点。
<?php
$orgdoc = new DOMDocument;
$orgdoc->loadXML("<root><element><child>text in child</child></element></root>"
// The node we want to import to a new document
$node = $orgdoc->getElementsByTagName("element")->item(0
// Create a new document
$newdoc = new DOMDocument;
$newdoc->formatOutput = true;
// Add some markup
$newdoc->loadXML("<root><someelement>text in some element</someelement></root>"
echo "The 'new document' before copying nodes into it:\n";
echo $newdoc->saveXML(
// Import the node, and all its children, to the document
$node = $newdoc->importNode($node, true
// And then append it to the "<root>" node
$newdoc->documentElement->appendChild($node
echo "\nThe 'new document' after copying the nodes into it:\n";
echo $newdoc->saveXML(
?>
上面的例子将输出:
The 'new document' before copying nodes into it:
<?xml version="1.0"?>
<root>
<someelement>text in some element</someelement>
</root>
The 'new document' after copying the nodes into it:
<?xml version="1.0"?>
<root>
<someelement>text in some element</someelement>
<element>
<child>text in child</child>
</element>
</root>
← DOMDocument::getElementsByTagNameNS
DOMDocument::load →