perl beginer...help with how to parse a xml file

newbie_10's Avatar
Light Poster
Hi,

I want to take an xml file and recognize a particular tag and print what is in between that tag.
How can I refer to something that is nested?

Example: Books.xml
<Books>

<Book>
<title>Perl Magic</title>
<author>Karthik</author>
<publisher>ORielly</publisher>
<price currency="Rupees" value="330"/>
</Book>

<Book>
<title>Perl for Dummies</title>
<author>Mark</author>
<publisher>ORielly</publisher>
<price currency="Rupees" value="420"/>
</Book>

</Books>

Perl script : books.pl

use XML::Simple;
use Data:: Dumper;

$xml = new XML::Simple;

$data = $xml->XMLin("books.xml");

print "$data->{author} is the author of $data->{title} and it is published by $data->{publisher}\n"


I'm not getting the correct output for this. What am I missing?
newbie_10's Avatar
Light Poster
I want the output like:

Karthik is the author of Perl Magic and it is published by ORielly.
Mark is the author of Perl for Dummies it is published by ORielly.


Can someone help me out?? please..

Thanks in advance
sakthiwin2004's Avatar, Join Date: Jul 2012
Newbie Member
use the following code,

print "$data->{Book}->[0]->{author} is the author of $data->{Book}->[0]->{title} and it is published by $data->{Book}->[0]->{publisher}\n"
print "$data->{Book}->[1]->{author} is the author of $data->{Book}->[1]->{title} and it is published by $data->{Book}->[1]->{publisher}\n"

instead of print "$data->{author} is the author of $data->{title} and it is published by $data->{publisher}\n"

Note: Suppose, input has 100 <Book> you are using the loop accordingly.

Thanks
Sakthi