Home » Php » php – Easiest way to check if node exist before getting attribute

php – Easiest way to check if node exist before getting attribute

Posted by: admin February 25, 2020 Leave a comment

Questions:

I need to get multiple values from nodes and their attributes using xpath. Some of them not always exist so i need to check before geting value. I know that i can write if statement for each of them to check length, but i wonder if there is a easier way to do it.

im getting data using this example:

$xpath = new DOMXPath($doc);

$xpath->query('/list/node[0]/@val')->item(0)->nodeValue;
$xpath->query('/list/node[0]/node')->item(0)->nodeValue;
How to&Answers: