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

Light Poster
6Jun2011,23:44   #1
newbie_10's Avatar
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?
Light Poster
7Jun2011,00:31   #2
newbie_10's Avatar
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
Newbie Member
4Jul2012,10:13   #3
sakthiwin2004's Avatar
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