I'm trying to parse a xml response from other server.
I can get my needed objects from that xml. but some times and some how, I cant get some objects. and this error appears.
Fatal error: Call to a member function getElementsByTagName() on a non-object in line 91
I checked every thing and I think there is nothing wrong.
here is an example xml response:
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0" xmlns:domain="http://epp.nic.ir/ns/domain-1.0">
<response xmlns:domain="http://epp.nic.ir/ns/domain-1.0">
<result code="1000">
<msg>Command completed successfully</msg>
</result>
<resData xmlns:domain="http://epp.nic.ir/ns/domain-1.0">
<domain:infData xmlns:domain="http://epp.nic.ir/ns/domain-1.0">
<domain:name>pooyaos.ir</domain:name>
<domain:roid>305567</domain:roid>
<domain:status s="serverHold"/>
<domain:status s="irnicReserved"/>
<domain:status s="serverRenewProhibited"/>
<domain:status s="serverDeleteProhibited"/>
<domain:status s="irnicRegistrationDocRequired"/>
<domain:contact type="holder">pe59-irnic</domain:contact>
<domain:contact type="admin">pe59-irnic</domain:contact>
.
.
and more...
and I am trying to get this object domain:infData
I think the error is from this part.
when I am trying to get this object, domdocument returns null.
php code:
function DinfData()
{
$data = $this->dom->getElementsByTagName("infData")->item(0);
91: $name = $data->getElementsByTagName("name")->item(0)->nodeValue;
$roid = $data->getElementsByTagName("roid")->item(0)->nodeValue;
$update = $data->getElementsByTagName("upDate")->item(0)->nodeValue;
$crdate = $data->getElementsByTagName("crDate")->item(0)->nodeValue;
$exdate = $data->getElementsByTagName("exDate")->item(0)->nodeValue;
and more...
I marked line 91 in error.
thanks ....
edit
$this->dom is my DOMDocument object and has no error.
If nothing is wrong is there any better way to get elements?
You need to use DOMDocument::getElementsByTagNameNS() which is for use with namespaced elements. Find the namespace that domain points to and pass it to the method along with the target tag name e.g. roid or name.
$dom = new DOMDocument();
$dom->loadXML($xml);
$ns = 'http://epp.nic.ir/ns/domain-1.0';
$data = $dom->getElementsByTagNameNS($ns, 'infData')->item(0);
$roid = $data->getElementsByTagNameNS($ns, 'roid')->item(0)->nodeValue;
$name = $data->getElementsByTagNameNS($ns, 'name')->item(0)->nodeValue;
// repeat the same for others
echo $roid . "\n";
echo $name;
Outputs
305567
pooyaos.ir
Simply put in your url into simple_load_file()
<?php
$xml = simplexml_load_file($xmlfile);
print $xml->infData; // Sample content
?>
This is a easiest method
But infData is not your tagName. This XML uses namespaces (domain is your namespace). Don't have experience with that so I really can help you any futher then the hint where to look. The getElementsByTagName is not going to work, you need something like DOMXPath I think that's where you can do something with namespaces.
Sorry I can't be of futher help but since no one mentioned the namespaces I thought I would just add the answer anyway. Maybe it will help you Google for an answer.
来源:https://stackoverflow.com/questions/18096972/how-to-parse-xml-namespaces-with-domdocument-and-be-sure-to-get-objects