Rob Dixon wrote:
Richard Lee wrote:
while trying to study the article on perlmonks.org,
http://perlmonks.org/?node_id=490846
regarding XML parsing, I need bit of clarfication.
how do I parse out
<image src="http://www.oreilly.com/catalog/covers/perlbp.s.gif"
width="145" height="190" />
I tried $book->{image}->{src}... but doesn't work.. I need some
understanding on how these information is stored.
parsing code
use XML::Simple qw(:strict);
my $library = XMLin($filename,
ForceArray => 1,
KeyAttr => {},
);
foreach my $book (@{$library->{book}}) {
print $book->{title}->[0], "\n"
}
XML file
<library>
<book>
<title>Perl Best Practices</title>
<author>Damian Conway</author>
<isbn>0596001738</isbn>
<pages>542</pages>
<image src="http://www.oreilly.com/catalog/covers/perlbp.s.gif"
width="145" height="190" />
</book>
<book>
<title>Perl Cookbook, Second Edition</title>
<author>Tom Christiansen</author>
<author>Nathan Torkington</author>
<isbn>0596003137</isbn>
<pages>964</pages>
<image src="http://www.oreilly.com/catalog/covers/perlckbk2.s.gi
+f"
width="145" height="190" />
</book>
<book>
<title>Guitar for Dummies</title>
<author>Mark Phillips</author>
<author>John Chappell</author>
<isbn>076455106X</isbn>
<pages>392</pages>
<image src="http://media.wiley.com/product_data/coverImage/6X/07
+645510/076455106X.jpg"
width="100" height="125" />
</book>
</library>
I agree with Stewart that XML::Simple is far from simple in practice. For the
selection of options for XMLin you have used, you can access the image data like
this:
foreach my $book (@{$library->{book}}) {
my $title = $book->{title}[0];
my $image = $book->{image}[0];
print "$title\n";
print " $image->{src}\n";
print " $image->{width}\n";
print " $image->{height}\n";
}
but the structure of the data will change depending on what options are set, and
in general it is very difficult to use XML::Simple without also using
Data::Dumper to inspect the data structure that has actually been generated.
HTH,
Rob
thanks all!
Will try few others now.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/