REST XML答案-Jaxb-亚马逊产品API [英] REST xml answer - Jaxb - Amazon product API

查看:127
本文介绍了REST XML答案-Jaxb-亚马逊产品API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前(正在尝试)使用亚马逊产品API来搜索这些商品。
我在XML中有响应,但是在jaxb中有一个例外,也许我错过了一些东西。

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..

这里是XML:

来自Amazon的XML响应

我想提取项目信息,但遇到了一些麻烦。

I want to extract items informations, but i'm getting some trouble.

项目类别:

@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 ();

the "response" come from a URL response.openStream();

好,我忘记了错误- _ -
javax.xml.bind.UnmarshalException:
意外元素(uri: http://webservices.amazon.com/AWSECommerceService/2011-08- 01 ,本地: ItemSearchResponse)。期望的元素是< {} ItemSearchResponse>

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>

谢谢!

推荐答案

该XML文档似乎是名称空间限定的。您可以使用包级别 @XmlSchema 位置注释来指定整个文档的名称空间限定。程序包级别的注释会进行特殊调用,称为 package-info ,如下所示:

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 called package-info that looks like the following:

package -info

@XmlSchema(
    namespace = "http://webservices.amazon.com/AWSECommerceService/2011-08-01",
    elementFormDefault = XmlNsForm.QUALIFIED)
package com.your.pkg;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

更多信息

  • http://blog.bdoughan.com/2010/08/jaxb-namespaces.html

这篇关于REST XML答案-Jaxb-亚马逊产品API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆