Check for property's existence without getting "Node no longer exists" warning
Do you want to check if your simplexml
contains a certain node or not? --> count them!
EDIT: errors when node doesn't exist --> try xpath:
if ($xml->xpath("//station")->Count()==0) echo "no station!";
will throw error if no station-node:
Say $xml
is your simplexml
and station
is on the top-level:
if ($xml->station->Count()==0) echo "no station!";
If 'station' is, say, a child of <something>
, you'd of course go...
... $xml->something->station->Count();
if (!$x || !$x->Station) { return false;}
Important thing - !$x
- you need check this xml object for empty. If it is true then on any try resolve $x->Station
you can get this error.
First check if by mistake you tried to access a property without checking before the bit of code you are trying to check (sometimes the error message is not accurate with the line numbers).
Then I used isset and it works fine:
if(isset($xml->element)) { // $xml has element }