A->b->c
might exist but c
might not exist. How do I check it?
After some experimentation, I've discovered that the only reliable method of checking if a node exists is using count($xml->someNode)
.
Here's a test case: https://gist.github.com/Thinkscape/6262156
Name Spaces
Be aware that if you are using name spaces in your XML file you will need to include those in your function calls when checking for children otherwise it will return ZERO every time:
if ($XMLelement->children($nameSpace,TRUE)->count()){
//do something here
}
I solved it by using the children()
function and doing a count()
on it, ignoring an PHP error if there are no children by putting an @ before the count-call. This is stupid, but it works:
$identification = $xml->identification;
if (@count($identification->children()) == 0)
$identification = $xml->Identification;
I hate this...
Simply
var_dump(count($xml->node));