2013-05-18 77 views
1

我想通過使用Yahoo Wheater(xml),xpath和php來了解城市的溫度。 這是我使用的代碼,但它不起作用。我可以得到一個元素(描述),但不是我想要的屬性(溫度)。在PHP中獲取xml屬性

$xml = new DOMDocument(); 
$xml->load("http://weather.yahooapis.com/forecastrss?w=12818432&u=c"); 

$xpath = new DOMXpath($xml); 
$result = $xpath->query("//channel"); 

foreach ($result as $value) { 
    //$weather = $value->getElementsByTagName("description"); 
    $weather = $value->getElementsByTagName("yweather:wind"); 
    $temp = $weather->getAttribute("chill"); 

    print $temp->item(0)->textContent; 
} 

回答

1

您應該使用getElementsByTagNameNS()來獲取名稱空間內的元素。

$ns_yweather = "http://xml.weather.yahoo.com/ns/rss/1.0"; 
$weer = $value->getElementsByTagNameNS($ns_yweather, "wind")->item(0); 
print $weer->getAttribute("chill");