Loop through XML::Simple structure
- by David
So I have some xml file like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<root result="0" >
<settings user="anonymous" >
<s n="blabla1" >
<v>true</v>
</s>
<s n="blabla2" >
<v>false</v>
</s>
<s n="blabla3" >
<v>true</v>
</s>
</settings>
</root>
I want to go through all the settings using the XML Simple.
Here's what I have when I print the output with Data::Dumper:
$VAR1 = {
'settings' => {
'user' => 'anonymous',
's' => [
{
'n' => 'blabla1',
'v' => 'true'
},
{
'n' => 'blabla2',
'v' => 'false'
},
{
'n' => 'blabla3',
'v' => 'true'
}
]
},
'result' => '0'
};
And here's my code
$xml = new XML::Simple;
$data = $xml->XMLin($file);
foreach $s (keys %{ $data->{'settings'}->{'s'} }) {
print "TEST: $s $data->{'settings'}->{'s'}->[$s]->{'n'} $data->{'settings'}->{'s'}->[$s]->{'v'}<br>\n";
}
And it returns these 2 lines, without looping:
TEST: n blabla1 true
TEST: v blabla1 true
I also tried to do something like this:
foreach $s (keys %{ $data->{'settings'}->{'s'} }) {
Without any success:
Type of arg 1 to keys must be hash (not array dereference)
How can I procede? What am I doing wrong?
Thanks a lot!