2010-07-16 57 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#"> 
相關問題