本文介绍了如何使用DOMDocument解析xml名称空间,并确保获取对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析其他服务器的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.

我检查了每件事,我认为没有错.

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指向的名称空间,并将其与目标标记名称一起传递给方法. roidname.

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;

输出

305567
pooyaos.ir

这篇关于如何使用DOMDocument解析xml名称空间,并确保获取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 17:54