0

I'm unable to retrieve data from tags with the : sign next to the name. It would be nice to have the avail attribute from the domain:nameelement and the text from the domain: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>

Addendum

If I run this

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

I get this result.

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

Why domain:tags are not consider as elements?

hakre
  • 193,403
  • 52
  • 435
  • 836
giannis christofakis
  • 8,201
  • 4
  • 54
  • 65

3 Answers3

2

You have a simple problem regarding the usage of XML namespaces, just take a look at this code snippet to workaround it:

<?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;

Output

1
Rolando Isidoro
  • 4,983
  • 2
  • 31
  • 43
0

Try this:

<?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";
     }
?>
achudars
  • 1,486
  • 15
  • 25
0

Here is a helpful blog.

And the solution I came out.

$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;

Result

1

Available Domain Name

Thanks to @ÁlvaroG.Vicario.

giannis christofakis
  • 8,201
  • 4
  • 54
  • 65