当前位置: 首页 > 知识库问答 >
问题:

UnMarshalException:意外元素(URI:“”

江航
2023-03-14

我在将XML响应从服务转换为POJO时遇到一个异常。XML如下所示:

javax.xml.bind.UnmarshalException: unexpected element (uri:""
, local:"ItemSearchResponse"). Expected elements are
<{http://webservices.amazon.com/AWSECommerceService/2011-08-01}ItemSearchResponse>

我是这样用的:

 Document response = getResponse(url);
 JAXBContext context = JAXBContext.newInstance(AmazonItem.class);
 Unmarshaller unMarshaller = context.createUnmarshaller();
 newItem = (AmazonItem) unMarshaller.unmarshal(response);

下面是我的文件的详细信息

包-info.java

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

package com.services.amazon;


import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
@XmlRootElement(name="ItemSearchResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class AmazonItem
{
    @XmlElement(name="Items")
    private Items items = null;
}
@XmlAccessorType(XmlAccessType.FIELD)
public class Items { 
    @XmlElement(name="Item")
    List<Item> items = new ArrayList();
}
@XmlAccessorType(XmlAccessType.FIELD)
public class Item {
    @XmlElement(name="ASIN")
    private String asin;

    @XmlElement(name="ItemAttributes")
    private ItemAttributes attributes;

    @XmlElement(name="ItemLinks")
    private List<ItemLinks> itemLinks;
}
@XmlAccessorType(XmlAccessType.FIELD)
public class ItemAttributes {
    @XmlElement(name="Title")
    private String title;

    @XmlElement(name="Actor")
    private List<String> actor;

    @XmlElement(name="ProductGroup")
    private String productGroup;
}

ItemLink.java

@XmlAccessorType(XmlAccessType.FIELD)
public class ItemLink {
    @XmlElement(name="Description")
    private String description;

    @XmlElement(name="URL")
    private String url;
}

ItemLinks.java

@XmlAccessorType(XmlAccessType.FIELD)
public class ItemLinks {
    @XmlElement(name="ItemLink")
    List<ItemLink> itemLinks;
}

共有1个答案

林国安
2023-03-14

错误消息表示您得到的XML文档如下所示:

<ItemSearchResponse>

而不是与您已映射的命名空间限定相匹配的如下所示:

<ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
 类似资料: