Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/forums/perl/)
-   -   perl beginer...help with how to parse a xml file (http://www.go4expert.com/forums/perl-beginerhelp-parse-xml-file-t25986/)

newbie_10 6Jun2011 23:44

perl beginer...help with how to parse a xml file
 
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 7Jun2011 00:31

Re: perl beginer...help with how to parse a xml file
 
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 4Jul2012 10:13

Re: perl beginer...help with how to parse a xml file
 
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


All times are GMT +5.5. The time now is 21:52.