Display array variable from SimpleXML
I have a very simple XML file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<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>
<Day 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>
With some help on here yesterday I got it to pull out Date and Energy, but now I want to display Day Num.
I did a print_r($xml); which shows:
SimpleXMLElement Object
(
[Day] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Num] => 4197
)
[Date] => 270611
[Energy] => 47
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Num] => 4219
)
[Date] => 190711
[Energy] => 202
)
So the Num field I want is in an array, but I cannot figure out how to extract it?
My current code is very simple, it is:
$xml = simplexml_load_file("DATA.XML");
foreach($xml->children() as $day开发者_如何学运维)
{
list($date, $energy) = $day->children();
echo ',Date: ' . $date . ', Energy: ' . $energy . '<br />';
}
How would I extract Day Num please?
Use: $day->attributes()->Num
See: SimpleXMLElement::attributes for further reading.
精彩评论