0

我无法从:名称旁边带有符号的标签中检索数据。拥有来自元素的avail属性和来自.domain:namedomain:reason

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
  <response>
    <result code="1000">
      <msg>Command completed successfully</msg>
    </result>
    <resData>
      <domain:chkData xmlns:domain="urn:ietf:params:xml:ns:domain-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:domain-1.0 domain-1.0.xsd">
        <domain:cd>
          <domain:name avail="1">sdfgsdf.gr</domain:name>
          <domain:reason>Available Domain Name</domain:reason>
        </domain:cd>
      </domain:chkData>
    </resData>
    <extension>
      <extdomain:resData xmlns:extdomain="urn:ics-forth:params:xml:ns:extdomain-1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:ics-forth:params:xml:ns:extdomain-1.1 extdomain-1.1.xsd">
        <extdomain:comment ref="sdfgsdf.gr">The Domain Name can be provisioned.</extdomain:comment>
      </extdomain:resData>
    </extension>
    <trID>
       <clTRID>ABC:cs-foh:10782</clTRID>
       <svTRID>212-123-176-gr</svTRID>
    </trID>
  </response>
</epp>

附录

如果我运行这个

    function iterate($xml , $space = '') {
        foreach($xml->children() as $child){
            echo $space.$child->getName();
            echo "<br />";
            iterate( $child , $space.'--' );
        }
    }

我得到这个结果。

response
--result
----msg
--resData
--extension
--trID
----clTRID
----svTRID

为什么domain:tags不被视为元素?

4

3 回答 3

2

关于 XML 命名空间的使用,您有一个简单的问题,只需查看以下代码片段即可解决它:

<?php
$xml = <<<XML
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
  <response>
    <result code="1000">
      <msg>Command completed successfully</msg>
    </result>
    <resData>
      <domain:chkData xmlns:domain="urn:ietf:params:xml:ns:domain-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:domain-1.0 domain-1.0.xsd">
        <domain:cd>
          <domain:name avail="1">sdfgsdf.gr</domain:name>
          <domain:reason>Available Domain Name</domain:reason>
        </domain:cd>
      </domain:chkData>
    </resData>
    <extension>
      <extdomain:resData xmlns:extdomain="urn:ics-forth:params:xml:ns:extdomain-1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:ics-forth:params:xml:ns:extdomain-1.1 extdomain-1.1.xsd">
        <extdomain:comment ref="sdfgsdf.gr">The Domain Name can be provisioned.</extdomain:comment>
      </extdomain:resData>
</extension>
  <trID>
    <clTRID>ABC:cs-foh:10782</clTRID>
    <svTRID>212-123-176-gr</svTRID>
  </trID>
  </response>
</epp>
XML;

$sxe = new SimpleXMLElement($xml);
$sxe->registerXPathNamespace('domain', 'urn:ietf:params:xml:ns:domain-1.0');
$domain_name = $sxe->xpath('//domain:name');

echo $domain_name[0]->attributes()->avail;

输出

1
于 2013-07-12T11:44:54.840 回答
0

尝试这个:

<?php
     $string = <<<XML
     <domain:name avail="1">sdfgsdf.gr</domain:name>
     XML;

     $xml = simplexml_load_string($string);
     foreach($xml->domain[0]->attributes() as $a => $b) {
         echo $a,'="',$b,"\"\n";
     }
?>
于 2013-07-12T11:31:58.470 回答
0

是一个有用的博客。

我出来的解决方案。

$xml = simplexml_load_string($xml_string);

$avail = $xml->response->resData
->children('urn:ietf:params:xml:ns:domain-1.0')
->chkData->cd->name->attributes()->avail;

$reason = (string) $xml->response->resData
->children('urn:ietf:params:xml:ns:domain-1.0')
->chkData->cd->reason;

echo $avail;
echo "<br />";
echo $reason;

结果

1

可用域名

感谢@ÁlvaroG.Vicario。

于 2013-07-12T13:35:19.823 回答