worldofwires
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->getElements ByTagName( "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.
I've tried a few workarounds like if isset and if($data=$doc->getElements
Thank is advance for any assistance or suggestions.
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.
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;
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.
ASKER
Thanks cxr,
Just to check I've got this, so in my code I can do $data=$doc->getElementsByT agName("ca r"); 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.
Just to check I've got this, so in my code I can do $data=$doc->getElementsByT
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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
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?