Uncaught exception #39;DOMException#39; with message #39;Hierarchy Request Error#39;(未捕获的异常“DOMException和消息“层次请求错误)
问题描述
在将子节点替换或添加到节点时出错.
I'm getting error while replacing or adding a child into a node.
必填:
我想把这个改成..
<?xml version="1.0"?>
<contacts>
<person>Adam</person>
<person>Eva</person>
<person>John</person>
<person>Thomas</person>
</contacts>
喜欢这个
<?xml version="1.0"?>
<contacts>
<person>Adam</person>
<p>
<person>Eva</person>
</p>
<person>John</person>
<person>Thomas</person>
</contacts>
错误是
致命错误:未捕获的异常DOMException"和消息层次请求错误"
Fatal error: Uncaught exception 'DOMException' with message 'Hierarchy Request Error'
我的代码是
function changeTagName($changeble) {
for ($index = 0; $index < count($changeble); $index++) {
$new = $xmlDoc->createElement("p");
$new ->setAttribute("channel", "wp.com");
$new ->appendChild($changeble[$index]);
$old = $changeble[$index];
$result = $old->parentNode->replaceChild($new , $old);
}
}
推荐答案
PHP 中的 Hierarchy Request Error with DOMDocument 表示您正在尝试将节点移动到本身.将其与下图中的蛇进行比较:
The error Hierarchy Request Error with DOMDocument in PHP means that you are trying to move a node into itself. Compare this with the snake in the following picture:
与您的节点类似.您将节点移动到自身中.这意味着,当您想用段落替换人时,该人已经是该段落的子项.
Similar this is with your node. You move the node into itself. That means, the moment you want to replace the person with the paragraph, the person is already a children of the paragraph.
appendChild() 方法已经有效地将人移出 DOM 树,它不再是一部分:
The appendChild() method effectively already moves the person out of the DOM tree, it is not part any longer:
$para = $doc->createElement("p");
$para->setAttribute('attr', 'value');
$para->appendChild($person);
<?xml version="1.0"?>
<contacts>
<person>Adam</person>
<person>John</person>
<person>Thomas</person>
</contacts>
伊娃已经走了.它的 parentNode 已经是段落了.
Eva is already gone. Its parentNode is the paragraph already.
因此,您首先要替换然后附加孩子:
So Instead you first want to replace and then append the child:
$para = $doc->createElement("p");
$para->setAttribute('attr', 'value');
$person = $person->parentNode->replaceChild($para, $person);
$para->appendChild($person);
<?xml version="1.0"?>
<contacts>
<person>Adam</person>
<p attr="value"><person>Eva</person></p>
<person>John</person>
<person>Thomas</person>
</contacts>
现在一切都很好.
这篇关于未捕获的异常“DOMException"和消息“层次请求错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:未捕获的异常“DOMException"和消息“层次请求错误"
基础教程推荐
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01