如何使用PHP中的DOMDocument检测XML元素是否为空? [英] How to Detect if an XML Element is Empty using DOMDocument in PHP?
问题描述
如果我删除一个元素内的一个元素,如果它是最后一个元素,那么父元素将缩短为空元素。
If I delete an element inside an element, and if its the last element, then the parent element gets shortened to an empty element.
示例:
开始:
< element>< child1>内部HTML< / child1>< child2>内部HTML< / child2>< / element>
我删除Child2并得到:
< element>< child1>内部HTML< / child1>< / element>
I delete Child2 and I get:
<element><child1>Inner HTML</child1></element>
我删除Child1并得到:
< element />
I delete Child1 and I get:
<element/>
现在,我希望这种情况不会发生,或者至少是一种检测元素是否为空的方法,这样我就可以代替它。
如何在PHP中使用DOMDocument做到这一点。
Now I want that to not happen or at least a way to detect if the element is empty so I can replace it. How can I do that using DOMDocument in PHP.
谢谢...
推荐答案
从 PHP手册中借用:
count($elem->children());
因此,检查是否为空:
$isEmpty = count($elem->children()) === 0 ? true : false;
这篇关于如何使用PHP中的DOMDocument检测XML元素是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!