开发者

Reading several elements from an XML feed with '-' in the element names

开发者 https://www.devze.com 2023-03-04 15:27 出处:网络
I am trying to read XML elements that have a \"-\" in the name. The feed can be found at http://forecast.weather.gov/MapClick.php?lat=42.19774&lon=-121.81797&FcstType=dwml In my last question

I am trying to read XML elements that have a "-" in the name. The feed can be found at http://forecast.weather.gov/MapClick.php?lat=42.19774&lon=-121.81797&FcstType=dwml In my last question I was just trying to read any of them. reading a XML feed with '-' in some of the element names Now I am trying to read a particular one(other than the first) and I am getting stumped again.

This will get me the first time-layout and the first start-valid-time.

$time = $xml->data->{'time-layout'}->{'start-val开发者_如何学Cid-time'};

I am after the second time-layout and I want to read through the attributes of the start-valid-time elements.

Below is a way that I have found that works. What I have done below cannot be the correct way to go about this. How should a person normally go about doing this?

Thanks.

     $time = $xml->data->{'time-layout'};
     $time2= $time[1]->{'start-valid-time'};
     $count= 14;
      for ($i = 0; $i <=$count ; $i++)
      {
         echo $time2[$i]->attributes();
         print "<br>\n";
      }


What you do is correct. You could shorten to

$dwml = simplexml_load_file('http://…');
foreach ($dwml->data->{'time-layout'}[1]->{'start-valid-time'} as $time) {
    echo $time;
}

or use XPath

$dwml = simplexml_load_file('http://…');
foreach ($dwml->xpath('/dwml/data/time-layout[2]/start-valid-time') as $time) {
    echo $time;
}


For this type of queries XPath is invented

0

精彩评论

暂无评论...
验证码 换一张
取 消