我目前(正在尝试)使用亚马逊产品 API 来搜索各种商品。我有 XML 中的响应,但我在 jaxb 中有一个例外,也许我错过了一些东西..
这是 XML:
我想提取项目信息,但我遇到了一些麻烦。
物品类别:
@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;
}
}
我的建设者:
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;
}
}
“响应”来自 URL response.openStream();
好的,我忘记了错误 - _ - javax.xml.bind.UnmarshalException:意外元素(uri:“ http://webservices.amazon.com/AWSECommerceService/2011-08-01 ”,本地:“ItemSearchResponse”)。预期元素是 <{}ItemSearchResponse>
谢谢 !