6.9 Working With Responses
Whether you're requesting information via URLs or SOAP requests, the
data you can access is the same, and it's sent in a very similar XML
format.
6.9.1 XML Parsers and XPath
To work with XML responses, you
need a piece of software, typically an add-on module, called an
XML parser. Almost every programming environment
has one; you just need to find out which one is available to you and
the syntax for using it. The parser's function is straightforward: to
load an XML document and make its contents available to your program
either as a data structure or a series of functions.
Once an XML document is loaded, you can get at the specific data it
contains with a simple query language called
XPath.
While parser syntaxes vary across languages and platforms, XPath is
rapidly gaining ground as a standard way to access the data. Here's a
quick example of how XPath queries work. If you request product
information, you need different pieces of that information at
different times. You can get to the specific information you need by
specifying a path through the document. Here's an abbreviated AWS
response:
<?xml version="1.0" encoding="UTF-8" ?>
<ProductInfo>
<Details url="http://amazon.com/o/0140042598">
<Asin>0140042598</Asin>
<ProductName>On the Road</ProductName>
<Catalog>Book</Catalog>
<Authors>
<Author>Jack Kerouac</Author>
</Authors>
</Details>
</ProductInfo>
As you can see, ProductInfo is the top-level
tag, and all other tags are within that
path. If you just wanted to get the product name, you could specify
the path ProductInfo/Details/ProductName. The
slashes represent levels of nesting, and the names are tag names.
Attributes are values located within a tag and
are accessed with the @ symbol. So, to get at URL
information, you could use the path
ProductInfo/Details/@url. You'll see XPath queries
like these in many of the hacks in this chapter, and, assuming the
availability of an XPath module, they work similarly across
development environments.
6.9.2 Lite Response and Heavy Response
Now let's take a look at the data that's
available through Amazon Web Services. As mentioned, you can request
either heavy or lite responses
depending on the value you set for the type
variable. Here's a list of information available in the
lite response, along with the XML path to access
it:
|
ASIN
|
Details/Asin
|
Product Name
|
Details/ProductName
|
Catalog
|
Details/Catalog
|
Author(s)
|
Details/Authors/Author
|
Release Date
|
Details/ReleaseDate
|
Manufacturer
|
Details/Manufacturer
|
Prices
|
Details/ListPrice, Details/OurPrice, Details/UsedPrice
|
Image URLs (small, medium, large)
|
Details/ImageUrlSmall, Details/ImageUrlMedium, Details/ImageUrlLarge
|
Request Arguments (the values you passed to AWS to make the request)
|
Request/Args/Arg
|
A heavy response includes all
the information in the
lite response, plus:
|
Sales Rank
|
Details/SalesRank
|
Lists (Listmania! lists that contain the product)
|
Details/Lists/ListId
|
Browse Node Names (categories)
|
Details/BrowseList/BrowseNode/BrowseName
|
Media
|
Details/Media
|
ISBN
|
Details/Isbn
|
Availability
|
Details/Availability
|
Reviews
|
Details/Reviews
|
Average Customer Rating
|
Details/Reviews/AvgCustomerRating
|
Total Customer Reviews
|
Details/Reviews/TotalCustomerReviews
|
Full Customer Reviews (includes rating, summary, and text of the
review)
|
Details/Reviews/CustomerReview/Rating,
Details/Reviews/CustomerReview/Summary,
Details/Reviews/CustomerReview/Comment
|
Similar Items (the ASINs of up to 5 similar items)
|
Details/SimilarProducts/Product
|
Now let's put this response data to work with some hacks!
|