This question already has an answer here:
- php SimpleXML check if a child exists 15 answers
I have this simplexml result object:
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) ""
}
}
}
}
I need to check if the node "problem_cause" exists. Even if it is empty, the result is an error. On the php manual, I found this php code that I modified for my needs:
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;
I have no idea what to put in place of the xpath query 'THE_PATH' to check if the node exists. Or is it better to convert the simplexml object to dom?