Pet*_*ley 29 php xml dom domdocument
有些东西我不完全了解使用PHP的DOM api进行节点克隆.这是一个示例文件,可以快速复制我遇到的问题.
$doc = new DOMDocument( '1.0', 'UTF-8' );
$root = $doc->createElement( 'root' ); // This doesn't work either $root = new DOMElement( 'root' );
$doc->appendChild( $root );
$doc2 = new DOMDocument( '1.0', 'UTF-8' );
$root2 = $doc2->createElement( 'root2' );
$doc2->appendChild( $root2 );
// Here comes the error
$root2->appendChild( $root->cloneNode() );
Run Code Online (Sandbox Code Playgroud)
当您运行此小片段时,会抛出异常
致命错误:未捕获的异常'DOMException',消息'Wrong Document Error'
我是否可以从文档中获取节点,克隆它,然后将其附加到另一个文档?
Gum*_*mbo 49
用于DOMDocument->importNode在将节点添加到DOM之前将节点导入到其他文档中.