Home
    Shop
    Advertise
    Write For Us
    Affiliate
    Newsletter
    Contact

Interpreting Data from Amazon's Web service

This is very basic level of tutorial in which I will explain how to fetch and read data from an web service. I expect that you have basic knowledge of XML and ASP.NET.

 

I will take the example of AMAZON's webservice, which is used by many retail sellers to get the updated information about product's (price, ASIN, SKU part # etc). What we will do is that we will query for some brand or product to web service exposed and it will return an XML document. That XML documents information and parameters (if exists) about product or brand. For example click the following link and you will be shown and XML document returned by web service when HP Laserjet 1018 printer was searched.

http://webservices.amazon.com/onca/xml?Service=AWSECommerceService& AWSAccessKeyId=0WMF85T7H0WQW4RNPZ82&Operation=ItemSearch &Keywords=HP%20Laserjet%201018%20Printer&SearchIndex=All& ResponseGroup=SalesRank,ItemAttributes,Images

Following figure shows the result of above query and you can notice that there are 5 <item> tags are returned for one product. The first <item> tag shows the details of our actual product and remaining <item> tags shows the products related to our product (Printer) like cartridge etc.

All these <item> tags are enclosed in <items> tag along with <Request> tag which contains information about our search query.

Now if you expand the first <Item> tag you will find that the tag contains lot of information about this product including its ASIN, Price, Images links, manufacturer, manufacturer part no and other attributes.

We are interested in these <item> tags only to either export them to excel file, or print them or to compare with our local values stored in database or even add to our database etc.

So let's get started and see how to get this document in ASP.NET 2.0 and parse these item tags etc. I am creating an console application which will query this product and will print the details of item returned.

First of all, include the following namespaces in your file:

using System;
using System.Xml;
using System.Collections;

and following class level variables in your code.

XmlDocument xmlDoc;
XmlNodeList xmlNode;
XmlNode node;

Now first of all we have to setup our URL based on our search criteria, for that I have declared a variable which contains my search keywords as follows

string productName = "HP Laserjet 1018 Printer";

You can setup search keywords from either database or some file etc. The url is contained in the variable url which contains the address of webservice and other parameters including our search keywords.

string url = "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId =0WMF85T7H0WQW4RNPZ82&Operation=ItemSearch&Keywords=" + productName + "&SearchIndex=All&ResponseGroup=SalesRank,ItemAttributes,Images";

Now we have to fetch the XML document from a webservice using above URL.

Uri uri = new Uri(url);

System.Uri class used to identify a resource located on a computer. A resource can anything, document, image, webservice etc. In our case it is a web service and returns a XML document so we collected that document in XMLDocument object 'xmlDoc' using its Load() method as follows.

xmlDoc.Load(uri.ToString());

Now XML document has been successfully loaded in our object and now we want to see whether our search returned some Items or not. GetElementByTagName() method of XMLDocument object returns the node list from the XML document.

xmlNode = xmlDoc.GetElementsByTagName("Item");

and we see wether we got some item or not by checking

if (xmlNode.Count >= 1)
{
    Console.WriteLine(xmlNode.Count.ToString() + " Items were found  for " + productName);
    ParseNodes();
}

Once we found that there exist one or more than one item, we have to parse them. For that I wrote a function called ParseNodes() which iterates through all the returned items.

protected static void ParseNodes()
{
    for (int nodeIndex = 0; nodeIndex < xmlNode.Count; nodeIndex++)
    {
        // More Code will be added in a little while
    }
}

Now I have used IEnumerator class to store the collection of attributes under each item as follows and the MoveNext() method for iterating through attributes.

protected static void ParseNodes()
{
    for (int nodeIndex = 0; nodeIndex < xmlNode.Count; nodeIndex++)
    {
        IEnumerator ienum = xmlNode[nodeIndex].GetEnumerator();
 
        while (ienum.MoveNext())
        {
          // More Code will be added in a little while   
        }
    }
}

Up till now we have reached an item and ready to get the values of some attributes. In this example I will reading attributes like (ASIN, LargeImageURL, Manufacturer, ManufacturerPartNo, UPC)

Item > ASIN
Item > LargeImageURL
Item > ItemAttributes > Manufacturer
Item > ItemAttributes > ManufacturerPartNo
Item > ItemAttributes > UPC

protected static void ParseNodes()
{
    for (int nodeIndex = 0; nodeIndex < xmlNode.Count; nodeIndex++)
    {
        IEnumerator ienum = xmlNode[nodeIndex].GetEnumerator();
        while (ienum.MoveNext())
        {
            node = (XmlNode)ienum.Current;
            if (node.Name == "ASIN")
              ASIN = node.InnerText;
            if (node.Name == "LargeImage")
              largeImageURL = node.InnerText;
            if (node.Name == "ItemAttributes")
            {
                XmlNode childNode;
                for (int j = 0; j < node.ChildNodes.Count; j++)
                {
                    childNode = node.ChildNodes.Item(j);
                    if (childNode.Name == "Manufacturer")
                        manufacturer = childNode.InnerText;
                    else if (childNode.Name == "MPN")
                        manufactPartNo = childNode.InnerText;
                    else if (childNode.Name == "UPC")
                        UPC = childNode.InnerText;
                }
            }
        }
    }
    Console.WriteLine ("ASIN: {0}\nImageURL: {1}\n Manufacture: {2}\nManufacturer Part Number: {3}\nUPC: {4}",ASIN, largeImageURL, manufacturer, manufactPartNo, UPC);
}

This is a very basic procedure and will work best if are interested only in few attributes of an item. However things will get worst when we will try to get attributes from innermost tags. For that we have to modify our simple ParseNodes() method to a recursive one which will allow us not only to get all the elements but also narrow down as much as possible with a very compact piece of code.

This is my first article on this site and I will appreciate all your comments and suggestion for future. Thanks!

This tutorial is written by Mr. Rashid Idriz


Tutorial toolbar:  Tell A Friend  |  Add to favorites  |  Feedback  |