15.4 Introducing Amazon XML ServicesThe Amazon Web Services interface (v2.0) is designed to provide Amazon associates programmatic access to Amazon catalog data and search functionalities. Amazon Web Services is available in two flavors.
A standard SOAP Web Service. The application architecture here is SOAP RPC. Query and response data are all enclosed in SOAP messages. We will discuss SOAP in more detail in Chapters 16 and 17. A literal XML service. In this model, the query is encoded as parameters in the request URL. The Amazon server returns an XML document containing the response data. The tags and structure of the returned document are defined in DTD files provided by Amazon.
The Amazon service can operate in lite or heavy mode. A lite mode response is smaller but contains less information. Due to the bandwidth and processing power limits of mobile clients, we use the lite mode in this chapter. To use the Amazon service, you have to first obtain an authentication token from the Amazon Web site. Then, you encode query parameters into a URL string. For example, the following URL (without line breaks) http://xml.amazon.com/onca/xml?v=1.0 &t=webservices-20&dev-t=ABCD123456 &KeywordSearch=mobile%20java &mode=books&type=lite&page=1&f=xml specifies that a user identified by token ABCD123456 asks to search keywords mobile java in books store. Note that the space between keywords is encoded using %20 per URL encoding rules. The first page result is returned as a lite version, literal XML document. Listing 15.3 demonstrates the returned XML document. I have added some white spaces and replaced long strings with ellipses (...) to make it more readable. Listing 15.3. A sample Amazon search response message<ProductInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "http://xml.amazon.com/schemas/dev-lite.xsd"> <Details url="http://www.amazon.com/exec/obidos/..."> <Asin>0471034657</Asin> <ProductName> Mobile Information Device Profile for Java 2 Micro Edition (J2ME): Professional Developer's Guide </ProductName> <Catalog>Book</Catalog> <Authors> <Author>C. Enrique Ortiz</Author> <Author>Eric Giguere</Author> </Authors> <ReleaseDate>15 January, 2001</ReleaseDate> <Manufacturer> John Wiley & Sons </Manufacturer> <ImageUrlSmall> http://images.amazon.com/...THUMBZZZ.jpg </ImageUrlSmall> <ImageUrlMedium> http://images.amazon.com/...MZZZZZZZ.jpg </ImageUrlMedium> <ImageUrlLarge> http://images.amazon.com/...LZZZZZZZ.jpg </ImageUrlLarge> <ListPrice>\$49.99</ListPrice> <OurPrice>\$49.99</OurPrice> <UsedPrice>\$28.99</UsedPrice> </Details> <Details url="..."> ... ... </Details> ... ... </ProductInfo> Our sample MIDlet AmazonLite demonstrates how to parse the Amazon lite document. AmazonLite first prompts the user for the keywords to search. Then, the user clicks which XML parsing mode she would like to test. Button Pull is for XmlPull, and button kDOM is for a document model mode. MIDlet then sends out the query, receives data, parses the response document, and then displays extracted data in a new form. Figure 15.4 shows the program in action. Figure 15.4. Mobile access to Amazon XML search interface.XML parsing in AmazonLite is done by the kXML parser. kXML is an open source XML parser that is compatible with all J2ME platforms. It is developed under the Endrya ME project and released under the CPL license. kXML v2.0 supports the XmlPull interfaces as well as the kDOM document model. |