我有这个 simplexml 结果对象:
object(SimpleXMLElement)#207 (2) {
["@attributes"]=>
array(1) {
["version"]=>
string(1) "1"
}
["weather"]=>
object(SimpleXMLElement)#206 (2) {
["@attributes"]=>
array(1) {
["section"]=>
string(1) "0"
}
["problem_cause"]=>
object(SimpleXMLElement)#94 (1) {
["@attributes"]=>
array(1) {
["data"]=>
string(0) ""
}
}
}
}
我需要检查节点“problem_cause”是否存在。即使它是空的,结果也是错误的。在 php 手册中,我找到了我根据需要修改的 php 代码:
function xml_child_exists($xml, $childpath)
{
$result = $xml->xpath($childpath);
if (count($result)) {
return true;
} else {
return false;
}
}
if(xml_child_exists($xml, 'THE_PATH')) //error
{
return false;
}
return $xml;
我不知道用什么代替 xpath 查询“THE_PATH”来检查节点是否存在。还是将simplexml对象转换为dom更好?