Processing Overview

In the previous section, you saw that an Amazon Associates Web Service response is an XML document. The elements returned and their values depend on the data stored in Amazon's databases and the response groups specified in the request.

A response group tailors the information returned in a response. For example, the Images response group returns the images of items returned in the response. The TopSellers response group returns the top selling items in a search index. The Amazon Associates Web Service Developer Guide lists all of the elements that a response group can potentially return.

The elements returned by a response group are structured. For example, the Item element has several child elements, one of which is Offers, which has a child element, Subtotal, which itself has three child elements: Amount, CurrencyCode, and FormattedPrice, as shown in the following response snippet..

<Item>
  <Offers>
    <Subtotal>
      <Amount>999</Amount>
      <CurrencyCode>US</CurrencyCode>
      <FormattedPrice>$9.99</FormattedPrice>

The parent-to-child succession of structured elements is called an XPath. To parse a result, the Amazon Associates Web Service response is turned into an object and then XPaths are used as an efficient means of finding elements and their values.

The following PHP example shows how an XPath is used to display the FormattedPrice value after first making sure there is a value.

if(isset($current->Item->Offers->Subtotal->FormattedPrice)){   print("<br>Price:"
    $current->Offers->Subtotal->FormattedPrice);

The similar expression in C# is:

Label1.Text += "Price: " + 
  Item.Offers.Subtotal.FormattedPrice + "<br />"; 

Returning additional values just requires the use of different XPaths.

Typically, responses return more than one item. For that reason, the parsing algorithm must iterate through all of the items returned in a response. For example, in PHP:

foreach($parsed_xml->Items->Item as $current){...}

In C#:

foreach(Item item in response){...}