javax.xml.bind.UnmarshalException: 意外元素 (uri:""
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22360943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javax.xml.bind.UnmarshalException: unexpected element (uri:""
提问by Ethan
I am getting an exception while turning an XML response from a service to a POJO. The XML looks like this:
将 XML 响应从服务转换为 POJO 时出现异常。XML 如下所示:
Here is my XML response.
这是我的XML 响应。
javax.xml.bind.UnmarshalException: unexpected element (uri:""
, local:"ItemSearchResponse"). Expected elements are
<{http://webservices.amazon.com/AWSECommerceService/2011-08-01}ItemSearchResponse>
I am using it like this:
我是这样使用它的:
Document response = getResponse(url);
JAXBContext context = JAXBContext.newInstance(AmazonItem.class);
Unmarshaller unMarshaller = context.createUnmarshaller();
newItem = (AmazonItem) unMarshaller.unmarshal(response);
Below are the details of my files
以下是我的文件的详细信息
package-info.java
包信息.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;
AmazonItem.java
亚马逊商品.java
@XmlRootElement(name="ItemSearchResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class AmazonItem
{
@XmlElement(name="Items")
private Items items = null;
}
Items.java
项目.java
@XmlAccessorType(XmlAccessType.FIELD)
public class Items {
@XmlElement(name="Item")
List<Item> items = new ArrayList();
}
Item.java
项目.java
@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;
}
ItemAttributes.java
项目属性.java
@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
项目链接.java
@XmlAccessorType(XmlAccessType.FIELD)
public class ItemLink {
@XmlElement(name="Description")
private String description;
@XmlElement(name="URL")
private String url;
}
ItemLinks.java
项目链接.java
@XmlAccessorType(XmlAccessType.FIELD)
public class ItemLinks {
@XmlElement(name="ItemLink")
List<ItemLink> itemLinks;
}
回答by bdoughan
The error message is saying that you are getting an XML document that looks like this:
错误消息是说您正在获取一个如下所示的 XML 文档:
<ItemSearchResponse>
Instead of one like the following that matches the namespace qualification that you have mapped:
而不是与您已映射的命名空间限定匹配的以下内容:
<ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
回答by hoaz
The explanation is here: The JAXBContext instance is intialized with class(es) passed as parameter(s) and classes that are statically reachable from these class(es).
解释在这里:JAXBContext 实例初始化为作为参数传递的类和可以从这些类静态访问的类。
Initialize JAXBContext
using package, so it can see @XmlSchema
declared in package-info.java:
JAXBContext
使用 package初始化,所以可以看到@XmlSchema
package-info.java 中声明的:
JAXBContext.newInstance("com.services.amazon")
回答by dmarwick
If you're using DocumentBuilderFactory in your getResponse method, try setting namespace awareness:
如果您在 getResponse 方法中使用 DocumentBuilderFactory,请尝试设置命名空间感知:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
I had the same UnmarshalException and this solved it.
我有相同的 UnmarshalException,这解决了它。
回答by Stack overflow user
Remove the namespace from package-info.java
and change
从中删除命名空间package-info.java
并更改
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED
to
到
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED.
回答by Daísa Fernandes
Remove the namespace from package-info.java It's work for me
从 package-info.java 中删除命名空间它对我有用
Ex:
前任:
@javax.xml.bind.annotation.XmlSchema(namespace = "", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)