php使用DOMDocument的时候如何判断xml中某节点是否存在

时间:2022-10-20 18:50:38

php利用DOMDocument处理XML文件时,有时候需要判断某个节点是否存在,但是DOMDocument并没有专门的方法去做这样的判断,可以通过判断该节点的长度去判断该节点是否存在,如果该节点长度等于0,则该节点不存在,如果不等于0,则该节点存在。

$dom = new DOMDocument();
$dom->load(/**/**.xml);
$node = $dom->getElementsByTagName('tac');
if($node->length == 0) {
echo "the node is not exists";
} else {
echo "the node is existed";
}