本文介绍了如何通过标记名称删除其中包含所有元素的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在php中使用dom removeChild函数删除标记之间的所有内容.
I want to use the dom removeChild function in php to remove everything between a tag.
我的xml看起来像
<root>
<element>text</element>
<remove>
text
<morexml>text</morexml>
</remove>
</root>
现在,我想删除包含整个标签在内的标签.我该怎么做呢?我没有头绪.我正在尝试使用发现的唯一dom功能:removeChild.
Now I want to remove the tag including its entire inside. How do I do this? I do not have a clue. I am trying to use the only dom function i found: removeChild.
删除后,它必须看起来像这样:
When removed it has to look like this:
<root>
<element>text</element>
</root>
是否有php dom函数可以做到这一点?我在Google或stackoverflow上找不到它.
Is there a php dom function to do this? I can not find it on google or stackoverflow.
推荐答案
$dom = new DOMDocument;
$dom->loadXML($xml);
$dom->getElementsByTagName('root')->item(0)
->removeChild($dom->getElementsByTagName('remove')->item(0));
这是非常具体的.如果需要更多通用性,可以使用XPath:
This is very specific, though. You can use XPath if you need more generality:
foreach ($xpath->query('//remove') as $node) {
$node->parentNode->removeChild($node);
}
这篇关于如何通过标记名称删除其中包含所有元素的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!