我有以下使用命名空间的XML文件。
当我使用以下php函数时,它不会返回aws的属性:Country Code
$xml = new SimpleXMLElement($response,null,false,
'http://awis.amazonaws.com/doc/2005-07-11')
所以我在想。如何提取国家/地区代码属性?我需要创建一个特殊功能吗?以下是返回curl调用的字符串。
<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>
答案 0 :(得分: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";
}
?>