Forum Moderators: buckworks

Message Too Old, No Replies

How to extract price from Amazon XML with Perl?

Can I access it directly?

         

MichaelBluejay

2:59 pm on Jun 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I just signed up for Amazon's Product Advertising API, and I got the sample Perl script working to send a request to look up an ISBN number and receive a response.

I can parse out the LowestUsedPrice (which is all I need) with a regular expression, but is there some way to access the XML field directly? I know nothing about XML, besides that it's a way to format data.

The XML is in a hash called $xml, which was created with these lines of the sample script:
my $xmlParser = new XML::Simple();
my $xml = $xmlParser->XMLin($content);

I'm sure someone here has done this before, right?

rocknbil

4:27 pm on Jun 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Add

use Data::Dumper;

Docs [search.cpan.org]

and read through the man pages for it, you'll be able to dump everything being returned in the XML string and it will show the tree structure. If it's not installed, cpan it . . .

The element you're looking for in the XML will be accessed something like

$my_target = $xml->{mainBranch}->{MiddleBranch}->{innerBranch};

Sometimes they are accessed as arrays if it's a list of items, i.e., for shipping API's, all items available for a set of parcel parameters:

foreach $e (@{$xml->{ArrayBranch}}) {
print $e->{InArray}->{Member};
}

MichaelBluejay

7:56 pm on Jun 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks. The script does already have use Data::Dumper, and print Dumper($xml) prints the whole tree. From the tree I'm able to see the path to the field I need, but I tried this and the variable is empty:
$my_target = $xml->{Items}->{Item}->{OffersSummary}->{LowestUsedPrice}->{FormattedPrice};
print $my_target;

I tried to see what would happen if I left off the last reference/branch/field (not sure what to call it) like so, and in that case, the output is "HASH(0x8634b38)".
$my_target = $xml->{Items}->{Item}->{OffersSummary}->{LowestUsedPrice};

So it seems like I'm getting close.

I did have a look at the CPAN doc but it's a bit over my head.

If I have to step through all the fields one at a time with a loop it seems that I might as well use a regexp.

jagathn

4:45 pm on Jul 6, 2010 (gmt 0)

10+ Year Member



Did this work for you? If you see "HASH(blahblah)" when you print it, then try print Dumper instead to see the structure of the HASH. Like this.

$my_target = $xml->{Items}->{Item}->{OffersSummary}->{LowestUsedPrice};
print Dumper $my_target;

That'll tell you all the key-value pairs in the hash itself. You can then navigate further into the hash and get to the value you are looking for.