Menu
Product Advertising API
Getting Started Guide (API Version 2013-08-01)

Product Advertising API Processing Implementations

The following sections show these parsing principles applied more robustly across several programming languages.

Java

In the previous section, the request retrieved a response object. The Product Advertising API Client-Side Library contains methods that can return a variety of values from that object. The following code retrieves from the response object the values of the following elements: items, item, item attributes, and title.

Copy
// Get the Title names of all the books for all the items returned in the response for (Items itemList : response.getItems()) { for (Item item : itemList.getItem()){ System.out.println("Book Name: " + item.getItemAttributes().getTitle()); } }

C#

The following C# code processes the response returned by Product Advertising API. This code is a continuation of the C# request sample code.

Copy
//Go through the response and display the //title, author, and price foreach (Items items in response.Items) { foreach (Item item in items.Item) { //Output the results to the console Console.WriteLine( "Title: " + item.ItemAttributes.Title + "\n" + "Author: " + item.ItemAttributes.Author + "\n" + "Price: " + item.ItemAttributes.ListPrice.FormattedPrice + "\n" ); } } } //Catch and display any exceptions catch (Exception ex) { Console.WriteLine("An error occurred: " + ex.ToString()); } Console.ReadLine(); } } }

This code uses a for statement to iterate through all of the items in the response. The title, author, and price element values are displayed using Console.WriteLine.

Perl

The following Perl code processes the response returned by Product Advertising API. This code is a continuation of the Perl request sample code.

Copy
# Process XML response using XPath (xp) my $xp = XML::XPath->new(xml => $response); # Iterate through the items in the response { for (my $i = 1; $i <= 10; $i++) { if ( ! $xp->find("/ItemSearchResponse/Items/Item[$i]") ) { last; } # Find author names my @authors; for (my $j = 1; $j <= $xp->findvalue("count(/ItemSearchResponse/Items/Item[$i]/ItemAttributes/Author)"); $j++) { push @authors, $xp->findvalue("/ItemSearchResponse/Items/Item[$i]/ItemAttributes/Author[$j]"); } # Find titles, prices, and display them with the authors print "Title: ", $xp->findvalue("/ItemSearchResponse/Items/Item[$i]/ItemAttributes/Title"), "\n", "Author: ", join(", ", @authors), "\n", "Price: ", $xp->findvalue("/ItemSearchResponse/Items/Item[$i]/Offers/Offer/OfferListing/Price/FormattedPrice"), "\n\n"; } }

This code uses a for statement to iterate through all of the items in the response. The title, author, and price element values are displayed using print.

PHP

The following PHP code processes the response returned by Product Advertising API. This code is a continuation of the PHP request sample code.

Copy
<?php function printSearchResults($parsed_xml, $SearchIndex){ print("<table>"); if($numOfItems>0){ foreach($parsed_xml->Items->Item as $current){ print("<td><font size='-1'><b>".$current->ItemAttributes->Title."</b>"); if (isset($current->ItemAttributes->Title)) { print("<br>Title: ".$current->ItemAttributes->Title); } elseif(isset($current->ItemAttributes->Author)) { print("<br>Author: ".$current->ItemAttributes->Author); } elseif (isset($current->Offers->Offer->OfferListing->Price->FormattedPrice)){ print("<br>Price: ".$current->Offers->Offer->OfferListing->Price->FormattedPrice); }else{ print("<center>No matches found.</center>"); } } } } ?>

The Product Advertising API response is put into an object, $parsed_xml, using the PHP command, simplexml_load_string. The response is displayed using the function, printSearchResults, which is defined as:

The code first checks to see if any items were returned in the response:

Copy
$numOfItems = $parsed_xml->Items->TotalResults; if($numOfItems>0){ ... }else{ print("<center>No matches found.</center>");

The item attributes are located using the XPath of the elements in the response: Items->TotalResults.

The code then parses the result object, $parsed_xml, by iterating over each item returned in the response. The Items->Item XPath is set to $current.

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

$current is used to access all of the item attributes in the response. For example, the following line displays the title:

Copy
print("<td><font size='-1'><b>".$current->ItemAttributes->Title."</b>");

The code only displays item attributes that are present in the response:

Copy
if(isset($current->ItemAttributes->Director)){ print("<br>Director: ".$current->ItemAttributes->Director);

The dot in the print statement concatenates the display of the Director attribute to all of the previous attributes displayed.

On this page: