Dom*_*oSL 1 php yahoo-weather-api
<?php
$doc = new DOMDocument();
$doc->load('http://weather.yahooapis.com/forecastrss?p=VEXX0024&u=c');
$channel = $doc->getElementsByTagName("channel");
foreach($channel as $chnl)
{
$item = $chnl->getElementsByTagName("item");
foreach($item as $itemgotten)
{
$describe = $itemgotten->getElementsByTagName("description");
$description = $describe->item(0)->nodeValue;
echo $description;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,这是一个简单的脚本,它从上面的url返回标记的内容.问题是我不需要那些内容,我需要标签内的人.我需要属性代码,temp,text.我如何用我的实际代码做到这一点?谢谢
标签内容的前缀:
<yweather:condition text="Partly Cloudy" code="30" temp="30" date="Fri, 16 Jul 2010 8:30 am AST" />
Run Code Online (Sandbox Code Playgroud)
就像是:
echo $itemgotten->getElementsByTagNameNS(
"http://xml.weather.yahoo.com/ns/rss/1.0","condition")->item(0)->
getAttribute("temp");
Run Code Online (Sandbox Code Playgroud)
关键是你必须使用getElementsByTagNameNS
而不是getElementsByTagName
并指定"http://xml.weather.yahoo.com/ns/rss/1.0"
为命名空间.
您知道这yweather
是一个快捷方式,http://xml.weather.yahoo.com/ns/rss/1.0
因为XML文件包含一个xmls
属性:
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0"
xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3236 次 |
最近记录: |