我正在尝试解析来自其他服务器的 xml 响应.
I'm trying to parse a xml response from other server.
我可以从那个 xml 中获取我需要的对象.但有些时候,有些如何,我不能得到一些对象.并出现此错误.
I can get my needed objects from that xml. but some times and some how, I cant get some objects. and this error appears.
致命错误:在第 91 行调用非对象上的成员函数 getElementsByTagName()
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.
这是一个示例 xml 响应:
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...
我正在尝试获取此对象 domain:infData
and I am trying to get this object domain:infData
我认为错误来自这部分.
I think the error is from this part.
当我试图获取这个对象时,domdocument 返回 null.
when I am trying to get this object, domdocument returns null.
php代码:
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...
我将第 91 行标记为错误.
I marked line 91 in error.
谢谢....
$this->dom
是我的 DOMDocument 对象,没有错误.
$this->dom
is my DOMDocument object and has no error.
如果没有问题,有没有更好的方法来获取元素?
If nothing is wrong is there any better way to get elements?
你需要使用 DOMDocument::getElementsByTagNameNS()
用于命名空间元素.找到 domain
指向的命名空间并将其与目标标记名称一起传递给方法,例如roid
或 name
.
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 . "
";
echo $name;
输出
305567
pooyaos.ir
这篇关于如何使用 DOMDocument 解析 xml 命名空间并确保获取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!