Rufnex has asked for the wisdom of the Perl Monks concerning the following question:

HiHo,

i try to parse the result of a amazon xml response via the webservice. if i call this service i got an xml Datafile like this:

<Details url="http://www.amazon.com/exec/obidos/redirect?tag=webser +vices-20%26creative=666%26camp=2025%26link_code=xm2%26path=ASIN/05960 +00480"> <Asin>0596000480</Asin> <Authors> <Author>David Flanagan</Author> </Authors> <Lists> <ListId>IEF1DNVKZO8B</ListId> <ListId>1R36BE3AUD988</ListId> <ListId>3S02XL0KTQDW0</ListId> </Lists> <BrowseList> <BrowseNode> <BrowseName>Computer Books: Languages</BrowseName> </BrowseNode> <BrowseNode> <BrowseName>Computer Programming Languages</BrowseName> </BrowseNode> </BrowseList> <Reviews> <AvgCustomerRating>4.31</AvgCustomerRating> <TotalCustomerReviews>178</TotalCustomerReviews> <CustomerReview> <Rating>5</Rating> <Summary>Excellent reference, not a "how to" book</Summary +> <Comment>... comment ...</Comment> </CustomerReview> <CustomerReview> <Rating>5</Rating> <Summary>Superb Reference</Summary> <Comment>... comment ...</Comment> </CustomerReview> </Reviews> </Details>

But now i will print out the informtion into a template. Can anyone give me a short eg. how to do this. i tried to do this with xml::simple like that:

$xmltree->{Node}

But for the deeper nested Nodes i've no idee how to catch it? And also the Attributes like "url" in the Details-Node.

Thx for any help ;o)

Rufnex

Replies are listed 'Best First'.
(jeffa) Re: How to parse this XML?
by jeffa (Bishop) on Jun 20, 2003 at 14:33 UTC
    You are going to probably need more than just a short example. You are going to need to read the fine manual: XML::Simple. But here is a script you can play with in the meantime:
    use strict; use warnings; use Data::Dumper; use XML::Simple; my $xml = XMLin('amazon.xml'); # print url attrib from <Details> print $xml->{Details}{url}, "\n"; # print each rating print $_->{Rating}, "\n" for @{ $xml->{Details}{Reviews}{CustomerRevie +w} }; # uncomment this line to print the entire data structure #print Dumper $xml;
    On another note, if you need to transform that XML into something else (a template?), then a better tool is most likely the XML::LibXML - XML::LibXSLT combo.

    There are so many options when it comes to parsing XML in Perl that it can be overwhelming at first. I recommend you buy a book: check out the site reviews for Perl & XML and XML and Perl. Also, give Ways to Rome a quick, shallow glance. It's probably too complex for you right now, but a quick glance will show you just how many options you have at your disposal. Good luck. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: How to parse this XML?
by gellyfish (Monsignor) on Jun 20, 2003 at 14:31 UTC

    My advice would be to feed your XML into a small program such as (assuming your XML is in 'text.xml' ) :

    use XML::Simple; use Data::Dumper; my $foo = XMLin('test.xml'); print Dumper($foo);
    Whereupon it should become clearer.
    /J\
    

Re: How to parse this XML?
by grantm (Parson) on Jun 20, 2003 at 18:04 UTC
Re: How to parse this XML?
by Rufnex (Novice) on Jun 20, 2003 at 21:25 UTC
    Thx to all ... i did it with xml::simple ;o) Rufnex