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 />';
}
精彩评论