2011-05-09 45 views
2

我正嘗試讀取在元素名稱中具有' - '的xml源。飼料可以在這裏找到。在某些元素名稱中使用' - '讀取XML Feed

http://forecast.weather.gov/MapClick.php?lat=42.19774&lon=-121.81797&FcstType=dwml

我是新來的PHP,所以我可能俯瞰一些基本的東西。我正在使用SimpleXML來閱讀提要。這裏是我用來找出我的問題的一些基本代碼。我讀了wordedForecast和min temp,沒有任何問題。當我嘗試閱讀的條件圖標的PHP不工作(這是我在底部註釋掉的部分),我相信這個問題的答案很簡單

<?php 
$feed_url = 'http://forecast.weather.gov/MapClick.php?lat=42.19774&lon=-121.81797&FcstType=dwml'; 
$ch = curl_init($feed_url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$result = curl_exec($ch); 
curl_close($ch); 

if (!empty($result)) 
{ 
$xml = new SimpleXMLElement($result); 
print "<br> Discription\n"; 
$wordedForecast = $xml->data->parameters->wordedForecast->text; 
foreach ($wordedForecast as $value) 
{ 
    print "<br> $value\n"; 
} 
print "<br> Min Tempature\n"; 
$Tempature = $xml->data->parameters->temperature->value; 
foreach ($Tempature as $value) 
{ 
    print "<br> $value\n"; 
} 
    print"<br>conditions Icon link\n"; 
//This is one of the elements that I am stumped on how to read. 
/*  $conditions= $xml->data->parameters->conditions-icon->icon-link; 
foreach ($conditions as $value) 
{ 
    print "<br> $value\n"; 
} 
*/ 
} 
?> 

,但我已用完關於如何去做的想法。我有與<time-layout>,<probability-of-precipitation><weather-conditions>元素相同的問題。

感謝您的任何答案或想法如何讓它工作。

+0

[用複姓SimpleXML的讀節點]的可能重複(http://stackoverflow.com/questions/3626901/simplexml-reading-node-with-a-hyphenated-name) – Gordon 2011-05-09 06:46:10

回答

1

嘗試$xml->data->parameters->{'conditions-icon'}->{'icon-link'}