0

I have the following XML file which uses namespaces.

When I use the following php function, it doesn't return the attributes for aws:Country Code

$xml = new SimpleXMLElement($response,null,false,
                            'http://awis.amazonaws.com/doc/2005-07-11')

So I was wondering. How can I pull out the Country Code attribute? Do I need to create a special function? The following is a string returned a curl call.

<aws:UrlInfoResponse xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/">
<aws:Response xmlns:aws="http://awis.amazonaws.com/doc/2005-07-11">
<aws:OperationRequest><aws:RequestId>e3459429-82f5-f598-0219-18a8056cad27</aws:RequestId>
</aws:OperationRequest>

<aws:UrlInfoResult>

<aws:Alexa>

<aws:TrafficData>
    <aws:DataUrl type="canonical">samplesite.com</aws:DataUrl>
    <aws:Rank>47216</aws:Rank>
      <aws:RankByCountry>
         <aws:Country Code="US">

            <aws:Rank>11438</aws:Rank>
            <aws:Contribution>
              <aws:PageViews>72.5%</aws:PageViews>
              <aws:Users>76.4%</aws:Users>
            </aws:Contribution>
      </aws:Country>
      <aws:Country Code="IN">
        <aws:Rank>45749</aws:Rank>
        <aws:Contribution>
          <aws:PageViews>17.0%</aws:PageViews>
          <aws:Users>7.5%</aws:Users>
        </aws:Contribution>
     </aws:Country>
    </aws:RankByCountry>
</aws:TrafficData>
</aws:Alexa>
</aws:UrlInfoResult>
</aws:Response>
</aws:UrlInfoResponse>         

1 Answer 1

1
<?php

$xml=<<<EOF
<aws:UrlInfoResponse xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/">
<aws:Response xmlns:aws="http://awis.amazonaws.com/doc/2005-07-11">
<aws:OperationRequest><aws:RequestId>e3459429-82f5-f598-0219-18a8056cad27</aws:RequestId>
</aws:OperationRequest>

<aws:UrlInfoResult>

<aws:Alexa>

<aws:TrafficData>
    <aws:DataUrl type="canonical">samplesite.com</aws:DataUrl>
    <aws:Rank>47216</aws:Rank>
      <aws:RankByCountry>
     <aws:Country Code="US">

        <aws:Rank>11438</aws:Rank>
        <aws:Contribution>
          <aws:PageViews>72.5%</aws:PageViews>
          <aws:Users>76.4%</aws:Users>
        </aws:Contribution>
      </aws:Country>
      <aws:Country Code="IN">
    <aws:Rank>45749</aws:Rank>
    <aws:Contribution>
      <aws:PageViews>17.0%</aws:PageViews>
      <aws:Users>7.5%</aws:Users>
    </aws:Contribution>
     </aws:Country>
    </aws:RankByCountry>
</aws:TrafficData>
</aws:Alexa>
</aws:UrlInfoResult>
</aws:Response>
</aws:UrlInfoResponse>
EOF;

$sxe = new SimpleXMLElement($xml, null, false, 'http://awis.amazonaws.com/doc/2005-07-11');
foreach($sxe->xpath('//*[@Code]') as $node) {
        if($node->getName() != 'Country') continue;
        echo $node->attributes()->Code . "\n";
}

?>
Sign up to request clarification or add additional context in comments.

2 Comments

doesn't work for me because $xml doesn't return the attributes. How do I read it to traverse the xml file to get the tags and the attributes..my solution just gets the tags. Your solution doesn't do anything for me.
thanks that works..when I tried it just with what you sent it didn't..now it works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.