I'm currently (trying) to work with the amazon product API to search thourgh items. I have the response in XML, but i have an exception in jaxb, maybe i missed something..
Here is the XML :
I want to extract items informations, but i'm getting some trouble.
Item class:
@XmlRootElement(name="ItemSearchResponse")
public class AmazonItem
{
private String name;
private String asin;
private String price;
public AmazonItem()
{
}
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlElement(name="Title")
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlElement(name="ASIN")
public String getAsin()
{
return asin;
}
public void setAsin(String asin)
{
this.asin = asin;
}
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlElement(name="FormattedPrice")
public String getPrice()
{
return price;
}
public void setPrice(String price)
{
this.price = price;
}
}
my builder :
public class AmazonItemBuilder
{
public AmazonItemBuilder()
{
}
public List<AmazonItem> build(InputStream response)
{
try
{
JAXBContext context = JAXBContext.newInstance(AmazonItem.class);
Unmarshaller unMarshaller = context.createUnmarshaller();
AmazonItem newItem = (AmazonItem) unMarshaller.unmarshal(response);
System.out.println(newItem);
}
catch (JAXBException e)
{
e.printStackTrace();
}
return null;
}
}
the "response" come from a URL response.openStream();
OK i forgot the error -_- javax.xml.bind.UnmarshalException: unexpected element (uri:"http://webservices.amazon.com/AWSECommerceService/2011-08-01", local:"ItemSearchResponse"). Expected elements are <{}ItemSearchResponse>
Thank you !
I hope the top level class to covert the response to object should be "ItemSearchResponse", try creating a class with member variable "Items" which inturn will have another member object array "AmazonItem"
It appears that the XML document is namespace qualified. You can use the package level
@XmlSchema
location annotation to specify the namespace qualification for the entire document. Package level annotations go on a special call calledpackage-info
that looks like the following:package-info
For More Information