【发布时间】:2019-09-12 10:36:39
【问题描述】:
我想将一个 XML 文档中的 <w:p> 标记复制到另一个文档中。两个 XML 文档都遵循以下结构:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:main=""here is some namespace definitions">
<w:body>
<w:p>
<somechildelementshere />
</w:p>
</w:body>
</w:document>
我有这个PHP 代码:
// $targetDocument contains a <w:document> tag with their children
$target_body = $targetDocument->getElementsByTagNameNS($ns, 'body')[0];
// $sourceBody contains a <w:body> tag with their children
$paragraphs = $sourceBody->getElementsByTagNameNS($ns, 'p');
// $target_body is a DOMElement and $paragraph will be a DOMElement too
foreach ($paragraphs as $paragraph) {
$target_body->importNode($paragraph, true);
}
在 foreach 中我收到 DOMException Wrong Document Error 消息。
如何将一个 DOMElement 作为子元素添加到另一个元素中?
【问题讨论】:
-
importNode是DOMDocument的一种方法,但您正试图在单个元素节点上使用它……
标签: php xml xml-parsing domdocument