我正在为我的学校网站开发当前天气和天气预报的天气预报。为此,我将使用雅虎RSS天气预报。在这个XML文件中,有一些值存储在属性中。我想用PHP解决这些问题。该文件可以在这里找到:http://weather.yahooapis.com/forecastrss?w=12602818& u = c
XML文件声明如下行:
<rss xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0">
我想要输出的值如下:
<yweather:condition text="Partly Cloudy" code="30" temp="22" date="Wed, 12 Jun 2013 4:20 pm CEST"/>
例如,我想从这个yweather:条件中接收'temp'。有谁能告诉我如何用PHP做到这一点? <?php
$doc = new DOMDocument();
$doc->load('http://weather.yahooapis.com/forecastrss?w=12602818&u=c');
$channel = $doc->getElementsByTagName("channel");
foreach($channel as $chnl){
$item = $chnl->getElementsByTagName("item");
foreach($item as $itemgotten){
$curtemp = $itemgotten->getElementsByTagNameNS("http://xml.weather.yahoo.com/ns/rss/1.0","condition")->item(0)->getAttribute("temp");
echo $curtemp;
}
}
?>
这个修复了这个问题!答案在这里找到:如何获取标签"