读取某些元素名称中带有“-"的XML提要 [英] reading a XML feed with '-' in some of the element names
问题描述
我正在尝试读取元素名称中带有-"的xml提要.提要可以在这里找到.
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
我是php的新手,所以我很可能会看一些基本的东西.我正在使用SimpleXML读取提要.这是一些我用来解决问题的基本代码.我没有任何问题地读了wordedForecast和min tempature.当我尝试阅读条件图标时,php不起作用(这是我在底部注释掉的部分)
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";
}
*/
}
?>
我敢肯定,答案很简单,但是我已经没有足够的想法去解决这个问题了.我对<time-layout>
,<probability-of-precipitation>
和<weather-conditions>
元素有同样的问题.
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.
推荐答案
尝试$xml->data->parameters->{'conditions-icon'}->{'icon-link'}
这篇关于读取某些元素名称中带有“-"的XML提要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!