<bdo id='RHlk8'></bdo><ul id='RHlk8'></ul>

      <small id='RHlk8'></small><noframes id='RHlk8'>

        <i id='RHlk8'><tr id='RHlk8'><dt id='RHlk8'><q id='RHlk8'><span id='RHlk8'><b id='RHlk8'><form id='RHlk8'><ins id='RHlk8'></ins><ul id='RHlk8'></ul><sub id='RHlk8'></sub></form><legend id='RHlk8'></legend><bdo id='RHlk8'><pre id='RHlk8'><center id='RHlk8'></center></pre></bdo></b><th id='RHlk8'></th></span></q></dt></tr></i><div id='RHlk8'><tfoot id='RHlk8'></tfoot><dl id='RHlk8'><fieldset id='RHlk8'></fieldset></dl></div>
        <legend id='RHlk8'><style id='RHlk8'><dir id='RHlk8'><q id='RHlk8'></q></dir></style></legend>
        <tfoot id='RHlk8'></tfoot>

        如何使用 DOMDocument 解析 xml 命名空间并确保获取对象

        时间:2023-11-30

        <small id='dy8R6'></small><noframes id='dy8R6'>

        <tfoot id='dy8R6'></tfoot>
        <legend id='dy8R6'><style id='dy8R6'><dir id='dy8R6'><q id='dy8R6'></q></dir></style></legend>
            <tbody id='dy8R6'></tbody>
            <i id='dy8R6'><tr id='dy8R6'><dt id='dy8R6'><q id='dy8R6'><span id='dy8R6'><b id='dy8R6'><form id='dy8R6'><ins id='dy8R6'></ins><ul id='dy8R6'></ul><sub id='dy8R6'></sub></form><legend id='dy8R6'></legend><bdo id='dy8R6'><pre id='dy8R6'><center id='dy8R6'></center></pre></bdo></b><th id='dy8R6'></th></span></q></dt></tr></i><div id='dy8R6'><tfoot id='dy8R6'></tfoot><dl id='dy8R6'><fieldset id='dy8R6'></fieldset></dl></div>

                <bdo id='dy8R6'></bdo><ul id='dy8R6'></ul>
                • 本文介绍了如何使用 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.

                  致命错误:在第 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 指向的命名空间并将其与目标标记名称一起传递给方法,例如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 . "
                  ";
                  echo $name;
                  

                  输出

                  305567
                  pooyaos.ir
                  

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

                  上一篇:使用多个命名空间解析 XML 下一篇:未捕获的异常“DOMException"和消息“层次请求错误"

                  相关文章

                    <i id='7nrax'><tr id='7nrax'><dt id='7nrax'><q id='7nrax'><span id='7nrax'><b id='7nrax'><form id='7nrax'><ins id='7nrax'></ins><ul id='7nrax'></ul><sub id='7nrax'></sub></form><legend id='7nrax'></legend><bdo id='7nrax'><pre id='7nrax'><center id='7nrax'></center></pre></bdo></b><th id='7nrax'></th></span></q></dt></tr></i><div id='7nrax'><tfoot id='7nrax'></tfoot><dl id='7nrax'><fieldset id='7nrax'></fieldset></dl></div>

                    <small id='7nrax'></small><noframes id='7nrax'>

                    <legend id='7nrax'><style id='7nrax'><dir id='7nrax'><q id='7nrax'></q></dir></style></legend>
                    <tfoot id='7nrax'></tfoot>
                    • <bdo id='7nrax'></bdo><ul id='7nrax'></ul>