未知的异常'DOMException'与消息'层次结构请求错误' [英] Uncaught exception 'DOMException' with message 'Hierarchy Request Error'
问题描述
需要的是:
我想把它改成..
<?xml version =1.0?>
< contacts>
< person> Adam< / person>
< person> Eva< / person>
< person> John< / person>
< person> Thomas< / person>
< / contacts>
喜欢这个
code><?xml version =1.0?>
< contacts>
< person> Adam< / person>
< p>
< person> Eva< / person>
< / p>
< person> John< / person>
< person> Thomas< / person>
< / contacts>
错误是
致命错误:未捕获的异常DOMException,消息为层次结构请求错误
我的代码是
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中使用 DOMDocument 的层次结构请求错误意味着您正在尝试将节点移动到自身中。请与以下图片中的蛇进行比较:
类似的是你的节点。将节点移动到本身。这意味着,当你想用该段替换人的那一刻,该人已经是该段落的孩子了。
appendChild()方法有效地将人移出DOM树,它不再是部分:
$ 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 已经是段落了。
所以,您首先要替换,然后追加孩子:
$ 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>
现在一切都很好。
I'm getting error while replacing or adding a child into a node.
Required is :
I want to change this to..
<?xml version="1.0"?>
<contacts>
<person>Adam</person>
<person>Eva</person>
<person>John</person>
<person>Thomas</person>
</contacts>
like this
<?xml version="1.0"?>
<contacts>
<person>Adam</person>
<p>
<person>Eva</person>
</p>
<person>John</person>
<person>Thomas</person>
</contacts>
error is
Fatal error: Uncaught exception 'DOMException' with message 'Hierarchy Request Error'
my code is
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);
}
}
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.
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>
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>
Now everything is fine.
这篇关于未知的异常'DOMException'与消息'层次结构请求错误'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!