问题
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