0
if($xml->getElementsByTagName($elmnt) && $xml->getElementsByTagName($elmnt)->length > 0)

This line is intended to check for errors. All I want is to, instead of breaking the entire page, make a legible error message. It is included in a function designed to stop all related processes on failure and continue displaying the rest of the page if it doesn't work, since the page layout does not depend on whether or not this function succeeds.

Fatal error: Call to a member function getElementsByTagName() on a non-object in file.php on line 100

How do I actually check to make sure that the DOMDocument in question has the element without it throwing the error above? I've tried using just the first condition or the second condition.

var_dump($xml);

object(DOMDocument)#3 (1) {
  ["preserveWhitespace"]=>
  bool(false)
}
Dissident Rage
  • 2,610
  • 1
  • 27
  • 33
  • Indeed this does not make any sense. Triple check that everything is as you describe it. Try breaking the two calls down into separate lines, that may also help debug the issue. – Jon Dec 13 '11 at 16:42
  • It looks more sensible to trigger an error message when you create `$xml` rather than every time you use it. If you edit the question and add that part of the code, we will be able to provide better advice. – Álvaro González Dec 13 '11 at 16:45
  • I think you need to include some sample XML as well – ajreal Dec 13 '11 at 16:46
  • It is for sure a DOMDocument. It does not include the element being sought after (this is intentional), but there is for sure an XML document being parsed. The point here isn't where it's best to find errors, but to prevent an error message nevertheless from destroying the page render. – Dissident Rage Dec 13 '11 at 16:57

2 Answers2

0

If you use simplexml_load_file to load the file, it will either return with a successfull load or it will return false on failure. The error you are getting signifies that the variable $xml is not an object, which means that when you tried to load the xml file onto the variable, it didn't do it successfully.

If before you do the getElementsByTagName($elmnt) you make sure $xml is not false, you shouldn't get the error Fatal error: Call to a member function getElementsByTagName() on a non-object in file.php on line 100

if (file_exists('test.xml')) {
    $xml = simplexml_load_file('test.xml');
    if($xml){
        print_r($xml);
    }
} else {
    exit('Failed to open test.xml.');
}
Jose Vega
  • 10,128
  • 7
  • 40
  • 57
  • The XML is a SOAP response. There is definitely a response - if there was no response, this part of the code would not be reached. – Dissident Rage Dec 13 '11 at 16:58
0

Your problem is that $xml is not an object, you have to check that first:

if ($xml && ($obj = $xml->getElementsByTagName($elmnt)) && $obj->length > 0) {
    // you should be good as this point
}

You can also use !empty($xml) instead of just $xml if there is a possibility that $xml is undefined.

netcoder
  • 66,435
  • 19
  • 125
  • 142