reading a XML feed with '-' in some of the element names

女生的网名这么多〃 提交于 2019-12-07 19:13:57

问题


I am trying to read an xml feed that has '-' in the element names. The feed can be found here.

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

I am new to php, so I am likely over looking something basic. I am using SimpleXML to read the feed. Here is some basic code that I am using to figure out my problem. I read the wordedForecast and min tempature with no problems. When I try to read the conditions-icon the php does not work(it is the part that I commented out at the bottom)

<?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";
}
*/  
}
?>

I am sure the answer to this is simple, but I have run out of ideas on how to go about it. I have the same problem with the <time-layout>,<probability-of-precipitation>, and <weather-conditions> elements.

Thanks for any answers or ideas on how to get it working.


回答1:


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



来源:https://stackoverflow.com/questions/5933234/reading-a-xml-feed-with-in-some-of-the-element-names

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!