2013-05-18 81 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");