I have an XML file that is thousands of lines long and I need to parse it out. I am attempting to use XML::Simple to read it in and put it into a Hash Array so that I can extract what I need. However, when I go to process with foreach I cant nail down the referenced data elements from the scalars. I have not had to code perl in a long while and I am very rusty and struggling to figure out how to get at the data elements in the hash that it creates for me. I used Data:umper (just initial lines printed) and an excerpt from the code pretty close to what the doc says for XML:Simple. Can you help direct me to what I am doing wrong? I know its a weakness in knowing how to use references withing the hash array lists. If I am just trying to get Number of Maple Trees from the {content} container below from Data:umper output, how would I get at it? I Have to dig deeper than this into the hash but if I can understand the easier piece then I should be able to get the deeper data. The error that I get on the foreach statement below is that it's "Not a HASH reference ". I have tried many different variations with the same error as result. Thanks in advance for any help/ direction.
use XML::Simple;
my $file = "my.xml";
$configs = XMLin($file);
foreach my $book (@{$configs->{'BOOK:BOOK'}->{'BOOK:Trees'}->{'BOOK:Tree'}->{'BOOK:Maple'}}) {
print "Maple has $book->{content} books \n";
}
(from Data:umper)
configs HASH below
$VAR1 = {
'BOOK:BOOK' => {
'BOOK:Trees' => {
'type' => 'Container',
'BOOK:Tree' => [
{
'BOOK:Maple' => {
'content' => '5',
'type' => 'Property'
},
'BOOK:Apple' => {
'content' => '16',
'type' => 'Property'
},
'BOOK:Oak' => {
'content' => '12',
'type' => 'Property'
},
'BOOK:Redwoods' => {
'BOOK:Redwood' => {
'content' => '342',
'type' => 'Property'
},
'type' => 'Category',
'BOOK:Favorites' => {
'BOOK:Favorite' => [
{
'BOOK:Children' => {
'content' => 11',
'type' => 'Property'
},
'BOOK:Adult' => {
'content' => 5',
'type' => 'Property'
},
use XML::Simple;
my $file = "my.xml";
$configs = XMLin($file);
foreach my $book (@{$configs->{'BOOK:BOOK'}->{'BOOK:Trees'}->{'BOOK:Tree'}->{'BOOK:Maple'}}) {
print "Maple has $book->{content} books \n";
}
(from Data:umper)
configs HASH below
$VAR1 = {
'BOOK:BOOK' => {
'BOOK:Trees' => {
'type' => 'Container',
'BOOK:Tree' => [
{
'BOOK:Maple' => {
'content' => '5',
'type' => 'Property'
},
'BOOK:Apple' => {
'content' => '16',
'type' => 'Property'
},
'BOOK:Oak' => {
'content' => '12',
'type' => 'Property'
},
'BOOK:Redwoods' => {
'BOOK:Redwood' => {
'content' => '342',
'type' => 'Property'
},
'type' => 'Category',
'BOOK:Favorites' => {
'BOOK:Favorite' => [
{
'BOOK:Children' => {
'content' => 11',
'type' => 'Property'
},
'BOOK:Adult' => {
'content' => 5',
'type' => 'Property'
},