Link to home
Start Free TrialLog in
Avatar of worldofwires
worldofwiresFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Retieving XML data

I'm using DomDocument to retrieve data from an XML file, my issue that the XML may not have all the data I'm requesting and consequently I'm getting errors along the lines of... Trying to get property of non-object.
I've tried a few workarounds like if isset and if($data=$doc->getElementsByTagName("pcap") but neither seem to be working. I'm not a complete idiot, what am I missing?
Thank is advance for any assistance or suggestions.
Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

>> Trying to get property of non-object.

This would happen when you try to read a property from something that is not an object. For example:

$a=0;
echo $a->b;

Could you post your code and some sample xml?
Avatar of worldofwires

ASKER

Hi, thanks for quick response.

I've included a snippet of the code I'm using and underneath the if statement is an example of what I've been trying to get around the issue. There is a sample xml file attached too, so if the car element for instance doesn't exisit, it would generate the error. I hope this makes sense.

if(file_exists('data/records/xml/'.$fn.'.xml')) {
  $doc = new DOMDocument();
  $doc->load('data/records/xml/'.$fn.'.xml');
 
  $data=$doc->getElementsByTagName("name");
  $arr['name']=array($data->item(0)->getAttribute("last"), $data->item(0)->getAttribute("first"));
 
  $data=$doc->getElementsByTagName("car");
  $arr['car']=$data->item(0)->nodeValue;
}
 
if($data=$doc->getElementsByTagName("car")) $arr['car']=$data->item(0)->nodeValue; else $arr['car']=0;

Open in new window

sample.xml.txt
getElementsByTagName() returns a DOMNodeList, this object has a "length" property. Check if this is non-zero before trying to fetch anything from it.
if(file_exists('data/records/xml/'.$fn.'.xml')) {
  $doc = new DOMDocument();
  $doc->load('data/records/xml/'.$fn.'.xml');
 
  $data=$doc->getElementsByTagName("name");
  if($data->length)
    $arr['name']=array($data->item(0)->getAttribute("last"), $data->item(0)->getAttribute("first"));
  else 
    $arr['name']=false;
 
  $data=$doc->getElementsByTagName("car");
  if($data->length)
    $arr['car']=$data->item(0)->nodeValue;
  else 
    $arr['car']=0;
}
 
# The problem with this:
if($data=$doc->getElementsByTagName("car")) 
  $arr['car']=$data->item(0)->nodeValue; else $arr['car']=0;
# ...is that getElementsByTagName() allways returns an object (DOMNodeList), even if no nodes was found.
# Thus, the if-statement is allways true.

Open in new window

Thanks cxr,

Just to check I've got this, so in my code I can do $data=$doc->getElementsByTagName("car"); And then do an IF statement saying if $data!=0 then use nodeValue, else it's 0?

It seems to work, I'm just clarifying I understand what's going on. Also, in your opinion, is there a problem with constantly reusing the $data value for each getElementByTagName?

Much appreciated.
ASKER CERTIFIED SOLUTION
Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks both,

I've used the SimpleXML on another XML function but got a little lost when using attributes. I like to keep things simple. :) I may rewrite but in teh meantime, xcr has solved my error generating problem.

Excellent, thaks guys. This site rocks.
Thanks for the points.  The rule of attributes is that they are array indexes.  See line 33 of the post above for the way to isolate those.  Best regards, ~Ray