Friday, 25 July 2014

dereferencing a XML::Simple hash



I have an XML file that looks like this



<booklist>
<book type="technical">
<author>Book 1 author 1</author>
<author>Book 1 author 2</author>
<title>Book 1 title</title>
<isbn>Book1ISBN</isbn>
</book>
<book type="fiction">
<author>Book 2 author 1</author>
<author>Book 2 author 2</author>
<title>Book 2 title</title>
<isbn>Book2ISBN</isbn>
</book>
<book type="technical">
<author>Book 3 author 1</author>
<author>Book 3 author 2</author>
<author>Book 3 author 3</author>
<title>Book 3 title</title>
<isbn>Book3ISBN</isbn>
</book>
</booklist>


When i put the file through a dumper - it looks like this:



#!/usr/bin/perl
use strict ;
use warnings ;
use XML::Simple ;
use Data::Dumper ;
my $book = ();

my $booklist = XMLin('book.xml_with_attrib');
print Dumper($booklist);

#foreach $book (@{$booklist->{author}} ) {
# print $book->{title} ;
# print "\n";
#}


This is the Dump:



/tmp/walt $ /tmp/walt/bookparse_by_attrib.pl
$VAR1 = {
'book' => [
{
'isbn' => 'Book1ISBN',
'title' => 'Book 1 title',
'author' => [
'Book 1 author 1',
'Book 1 author 2'
],
'type' => 'technical'
},
{
'isbn' => 'Book2ISBN',
'title' => 'Book 2 title',
'author' => [
'Book 2 author 1',
'Book 2 author 2'
],
'type' => 'fiction'
},
{
'isbn' => 'Book3ISBN',
'title' => 'Book 3 title',
'author' => [
'Book 3 author 1',
'Book 3 author 2',
'Book 3 author 3'
],
'type' => 'technical'
}
]
};


However when I try and print out the authors - this is what I get.



foreach $book (@{$booklist->{book}} ) {
print $book->{author} ;
print "\n";
}

ARRAY(0x249a140)
ARRAY(0x249a098)
ARRAY(0x2499fc0)


How would I print out author?


No comments:

Post a Comment