开发者

SimpleXML reading?

开发者 https://www.devze.com 2023-03-23 03:27 出处:网络
I have a very simple XML file, that looks like this: <Data><Day Num=\"4197\"> <Date>270611</Date>

I have a very simple XML file, that looks like this:

<Data><Day Num="4197">
<Date>270611</Date>
<Energy>47</Energy>
</Day>
<Day Num="4219">
<Date>190711</Date>
<Energy>202</Energy>
</Day>
<Day Num="4220">
<Date>200711</Date>
<Energy>17074</Energy>
</Day>
<Day Num="4221">
<Date>210711</Date>
<Energy>11563</Energy>
</Day>
<Day Num="4222">
<Date>220711</Date>
<Energy>18178</Energy>
</Day>
<Da开发者_开发问答y Num="4223">
<Date>230711</Date>
<Energy>24916</Energy>
</Day>
<Day Num="4224">
<Date>240711</Date>
<Energy>10555</Energy>
</Day>
<Day Num="4225">
<Date>250711</Date>
<Energy>12683</Energy>
</Day>
</Data>

I wrote this PHP to display the contents

$xml = simplexml_load_file("DATA.XML");

echo $xml->getName() . "<br />";

foreach($xml->children() as $child)
  {
  echo $child->getName() . ": " . $child . "<br />";
  }

However I don't get expected output, what I see is:

Data

Day:

Day:

Day:

Day:

I actually want to pull out just the Date and the Energy reading to write into a database, can anyone tell me how to amend my code to display date and Energy please?


foreach($xml->children() as $day)
{
    list($date, $energy) = $day->children();
    echo 'Date: ' . $date . ', Energy: ' . $energy . '<br />';
}
0

精彩评论

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