PHP Classes

Handle missing node/element?

Recommend this page to a friend!

      crXml  >  All threads  >  Handle missing node/element?  >  (Un) Subscribe thread alerts  
Subject:Handle missing node/element?
Summary:How to I stop fatal errors?
Messages:4
Author:Ken Williams
Date:2012-03-02 07:10:55
Update:2012-03-04 03:04:48
 

  1. Handle missing node/element?   Reply   Report abuse  
Picture of Ken Williams Ken Williams - 2012-03-02 07:10:55
I'm using Version 1.30 of crxml. I'm trying to get a value from my XML file. For example $aSomething -> Images[0]['count'];. This works fine if <Images count="1"></Images> exists. But if <Images> does not exist it fails with:
Fatal error: Call to a member function getNamedItem() on a non-object in /path/to/crXml.php on line 309

How do I stop this? How can I check if a node/element exists first so this fatal error isn't thrown?



  2. Re: Handle missing node/element?   Reply   Report abuse  
Picture of Sandeep.C.R Sandeep.C.R - 2012-03-02 08:32:35 - In reply to message 1 from Ken Williams
Hi ken,

Many thanks for pointing out this bug.

Actually you can call isset on a node to check for its existance.

But I have modified the code so that when a non existent node is accessed it will just return false.

I have uploaded the new version of file here at phpclasses and at its bitbucket repo at https://bitbucket.org/sandeepcr529/crxml

Hope this helps.

Thanks and regards,
Sandeep.

  3. Re: Handle missing node/element?   Reply   Report abuse  
Picture of Sandeep.C.R Sandeep.C.R - 2012-03-04 02:05:03 - In reply to message 1 from Ken Williams
Hi,

Kindly let me know if your problem was fixed.

Regards,
Sandeep

  4. Re: Handle missing node/element?   Reply   Report abuse  
Picture of Ken Williams Ken Williams - 2012-03-04 03:04:48 - In reply to message 3 from Sandeep.C.R
I don't know if it fixed my problem. I ended up using search() instead. For example:

$bCheckIfNodeExists = $aSomething -> search('NodeName');
if (strlen($bCheckIfNodeExists[0][somethingInNode]) > 4) : DoStuff(); endif;

This allowed me to see if it exists without throwing a fatal error. So I left it like this. Thank you for your help otherwise.