* Parsers a response to any Cart operation from Amazon.
* Returns the state of the Amazon's Cart.
*/
public Cart getCart(Document doc)
{
Cart cart = new Cart("", "");
CartItem cartItem = null;
Item item = null;
NodeList nodes, kids1, kids2 = null;
Node node, kid1, kid2 = null;
int i, j, k = 0;
nodes = doc.getElementsByTagName("CartItem");
for(i = 0; i < nodes.getLength(); i++)
{
cartItem = new CartItem();
item = new Item();
node = nodes.item(i);
kids1 = node.getChildNodes();
for(j = 0; j < kids1.getLength(); j++)
{
kid1 = kids1.item(j);
if(kid1.getNodeName().equals("ASIN"))
{
item.setAsin(kid1.getTextContent());
}
if(kid1.getNodeName().equals("Title"))
{
item.setTitle(kid1.getTextContent());
}
if(kid1.getNodeName().equals("CartItemId"))
{
cartItem.setCartItemId(kid1.getTextContent());
}
if(kid1.getNodeName().equals("Quantity"))
{
try
{
cartItem.setQuantity(Integer.parseInt(kid1.getTextContent()));
}
catch(NumberFormatException nfe)
{
cartItem.setQuantity(0);
}
}
if(kid1.getNodeName().equals("Price"))
{
kids2 = kid1.getChildNodes();
for(k = 0; k < kids2.getLength(); k++)
{
kid2 = kids2.item(k);
if(kid2.getNodeName().equals("FormattedPrice"))
{
cartItem.setUnitPrice(kid2.getTextContent());
}
}
}
if(kid1.getNodeName().equals("ItemTotal"))
{
kids2 = kid1.getChildNodes();
for(k = 0; k < kids2.getLength(); k++)
{
kid2 = kids2.item(k);
if(kid2.getNodeName().equals("FormattedPrice"))
{
cartItem.setTotalPrice(kid2.getTextContent());
}
}
}
}
cartItem.setItem(item);
cart.addItem(cartItem);
}
nodes = doc.getElementsByTagName("SubTotal");
for(i = 0; i < nodes.getLength(); i++)
{
node = nodes.item(i);
kids1 = node.getChildNodes();
for(j = 0; j < kids1.getLength(); j++)
{
kid1 = kids1.item(j);
if(kid1.getNodeName().equals("FormattedPrice"))
{
cart.setTotal(kid1.getTextContent());
}
}
}
nodes = doc.getElementsByTagName("PurchaseURL");
if(nodes.getLength() > 0)
{
cart.setUrl(nodes.item(0).getTextContent());
}
return cart;
}