我试图在父元素之前插入一些元素,如下所示:
$doc = new DOMDocument();
$doc->loadXML($this->_template);
$xpath = new DOMXpath($doc);
$blockNodeList = $xpath->query('//block[@name="'.$name.'"]');
foreach ($blockNodeList as $blockNode) {
if ($blockNode->hasChildNodes()) {
foreach ($blockNode->childNodes as $childNode) {
if ($childNode->nodeName != '#text') {
$blockNode->insertBefore($childNode, $blockNode);
}
}
}
}
但它只返回一个错误:未捕获的异常' DOMException'消息'未找到错误'
答案 0 :(得分:1)
您需要将其插入父元素。
$blockNode->parentNode->insertBefore($childNode, $blockNode);