2010-07-16 55 views
1
<?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; 
     } 
    } 
?> 

正如你所看到的是一个简单的脚本从上面的url返回标记的内容。问题是我不需要这些内容,我需要标签内的人。我需要的属性代码,临时,文本。我如何简单地使用我的实际代码?由于如何从PHP中的Yahoo Weather RSS获取标签“<yweather:condition>”?

防爆标签内容:

<yweather:condition text="Partly Cloudy" code="30" temp="30" date="Fri, 16 Jul 2010 8:30 am AST" /> 

回答

5

喜欢的东西:

echo $itemgotten->getElementsByTagNameNS(
    "http://xml.weather.yahoo.com/ns/rss/1.0","condition")->item(0)-> 
    getAttribute("temp"); 

的关键是,你必须使用getElementsByTagNameNS代替getElementsByTagName并指定"http://xml.weather.yahoo.com/ns/rss/1.0"作为命名空间。

你知道yweatherhttp://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#"> 
相关问题