Pawel Kopec
Pawel Kopec

Reputation: 31

XML parsing children problems

I'm working on a script that connects to a remote API and receives information in XML.

The problem I'm having is I can't to get parse domain:ns right.

XML below:

$xml= '<?xml version="1.0" encoding="UTF-8"?>
<response xmlns:domain="http://www.eurodns.com/domain" xmlns:eurodns="http://www.eurodns.com/eurodns">
 <extension>
  <eurodns:domain>
   <domain:ns addr="176.31.236.123">ns1.it-joan.pl</domain:ns>
   <domain:ns addr="89.72.43.135">ns2.it-joan.pl</domain:ns>
  </eurodns:domain>
 </extension>
</response>';

 $xml = new SimpleXMLElement($xml);
        foreach( $xml->resData as $value )
        {
                $array[] = $value->children('http://www.eurodns.com/eurodns');
        }

Upvotes: 3

Views: 135

Answers (1)

brazorf
brazorf

Reputation: 1951

The way to access namespaced items is like this

$xml= '<?xml version="1.0" encoding="UTF-8"?>
<response xmlns:domain="http://www.eurodns.com/domain" xmlns:eurodns="http://www.eurodns.com/eurodns">
 <extension>
  <eurodns:domain>
   <domain:ns addr="176.31.236.123">ns1.it-joan.pl</domain:ns>
   <domain:ns addr="89.72.43.135">ns2.it-joan.pl</domain:ns>
  </eurodns:domain>
 </extension>
</response>';
$xml = new SimpleXMLElement($xml);
$eurodns = $xml->extension->children('http://www.eurodns.com/eurodns');
$domain = $eurodns->children('http://www.eurodns.com/domain');

print_r($domain);

I don't know the entire xml structure you're workin on, or what you want to do with it, so i can't go anymore further. But you can start working your code from this, and from SimpleXMLElement::getNamespaces for a better dynamic parser.

Upvotes: 1

Related Questions