如何访问其中包含URL的节点?

时间:2012-05-29 22:07:40

标签: php xml-parsing weather-api

我有一些天气xml,我试图通过php循环解析。 问题是我能够访问除

之外的所有节点
<aws:weather xmlns:aws="http://www.aws.com/aws>

我无法向下钻取此节点。是因为网址? 我最后需要访问此节点

<aws:station> Salinas Municipal Airport <aws:station>

这是我的php:

<?php
$html = "";
$url = "http://i.wxbug.net/REST/SP/" .
       "getLiveWeatherRSS.aspx?api_key=sdfgdsgd5454&stationid=SLNAS";
$xml = simplexml_load_file($url);
for ($i = 0; $i < 10; $i++) {
$aws:station = $xml->channel->aws:weather->aws:ob[$i]->aws:station;
$html .= "<h3>$aws:station</h3>"; 
}
echo $html;
?>

这是xml

<rss xmlns:georss="http://www.georss.org/georss" version="2.0">
<channel>
<title>Observations from Salinas, CA - USA</title>
<link>...</link>
 <description>...</description>
<language>en-us</language>
<lastBuildDate>Tue, 29 May 2012 17:00:00 GMT</lastBuildDate>
<ttl>60</ttl>
 <aws:weather xmlns:aws="http://www.aws.com/aws">
<aws:api version="2.0"/>
<aws:WebURL>...</aws:WebURL>
<aws:InputLocationURL>...</aws:InputLocationURL>
<aws:ob>
<aws:ob-date>...</aws:ob-date>
<aws:requested-station-id>SLNAS</aws:requested-station-id>
<aws:station-id>KSNS</aws:station-id>
<aws:station>Salinas Municipal Airport</aws:station>
<aws:city-state zipcode="93905">Salinas, CA</aws:city-state>
<aws:country>USA</aws:country>
<aws:latitude>36.6633338928223</aws:latitude>
<aws:longitude>-121.608055114746</aws:longitude>
<aws:site-url/>
<aws:aux-temp units="&deg;F">0</aws:aux-temp>
<aws:aux-temp-rate units="&deg;F">-52.9</aws:aux-temp-rate>
<aws:current-condition icon="http://deskwx.weatherbug.com/images/Forecast/icons   
/cond000.gif">Clear</aws:current-condition>
<aws:dew-point units="&deg;F">45</aws:dew-point>
<aws:elevation units="ft">66</aws:elevation>
<aws:feels-like units="&deg;F"/>
<aws:gust-time>...</aws:gust-time>
<aws:gust-direction>WNW</aws:gust-direction>
<aws:gust-direction-degrees>281</aws:gust-direction-degrees>
<aws:gust-speed units="mph">N/A</aws:gust-speed>
<aws:humidity units="%">60</aws:humidity>
<aws:humidity-high units="%">6</aws:humidity-high>
<aws:humidity-low units="%">6</aws:humidity-low>
<aws:humidity-rate>0</aws:humidity-rate>
<aws:indoor-temp units="&deg;F">0</aws:indoor-temp>
<aws:indoor-temp-rate units="&deg;F">0</aws:indoor-temp-rate>
<aws:light>0</aws:light>
<aws:light-rate>0</aws:light-rate>
<aws:moon-phase moon-phase-img="http://api.wxbug.net/images/moonphase   
/mphase09.gif">-61</aws:moon-phase>
<aws:pressure units=""">30.08</aws:pressure>
<aws:pressure-high units=""">30.08</aws:pressure-high>
<aws:pressure-low units=""">30.05</aws:pressure-low>
<aws:pressure-rate units=""/h">0</aws:pressure-rate>
<aws:rain-month units=""">0</aws:rain-month>
<aws:rain-rate units=""/h">0</aws:rain-rate>
<aws:rain-rate-max units=""/h">0</aws:rain-rate-max>
<aws:rain-today units=""">0</aws:rain-today>
<aws:rain-year units=""">0</aws:rain-year>
<aws:temp units="&deg;F">59</aws:temp>
<aws:temp-high units="&deg;F">59</aws:temp-high>
<aws:temp-low units="&deg;F">51</aws:temp-low>
<aws:temp-rate units="&deg;F/h">0</aws:temp-rate>
<aws:sunrise>
<aws:year number="2012"/>
<aws:month number="5" text="May" abbrv="May"/>
<aws:day number="29" text="Tuesday" abbrv="Tue"/>
<aws:hour number="5" hour-24="05"/>
<aws:minute number="50"/>
<aws:second number="01"/>
<aws:am-pm abbrv="AM"/>
<aws:time-zone offset="-7" text="Pacific Daylight Time (USA)" abbrv="PDT"/>
</aws:sunrise>
<aws:sunset>
<aws:year number="2012"/>
<aws:month number="5" text="May" abbrv="May"/>
<aws:day number="29" text="Tuesday" abbrv="Tue"/>
<aws:hour number="8" hour-24="20"/>
<aws:minute number="18"/>
<aws:second number="10"/>
<aws:am-pm abbrv="PM"/>
<aws:time-zone offset="-7" text="Pacific Daylight Time (USA)" abbrv="PDT"/>
</aws:sunset>
<aws:wet-bulb units="&deg;F">51.602</aws:wet-bulb>
<aws:wind-speed units="mph">N/A</aws:wind-speed>
<aws:wind-speed-avg units="mph">N/A</aws:wind-speed-avg>
<aws:wind-direction>WNW</aws:wind-direction>
<aws:wind-direction-degrees>279</aws:wind-direction-degrees>
<aws:wind-direction-avg>WNW</aws:wind-direction-avg>
</aws:ob>
</aws:weather>
<image>...</image>
<item>...</item>
</channel>
</rss>

1 个答案:

答案 0 :(得分:1)

问题是由XML文档中的aws命名空间引起的。

<aws:weather xmlns:aws="http://www.aws.com/aws">

您可以使用childeren方法获取属于命名空间的所有节点,而不是访问这些节点。

您可以在http://www.sitepoint.com/simplexml-and-namespaces/

找到更详细的问题说明