Javax.xml.bind.UnmarshalException: unexpected element (uri: ""

I get an exception when you send an XML response from a service to POJO. XML looks like this:

Here is my XML answer .

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

I use it as follows:

  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

 @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

 @XmlRootElement(name="ItemSearchResponse") @XmlAccessorType(XmlAccessType.FIELD) public class AmazonItem { @XmlElement(name="Items") private Items items = null; } 

Items.java

 @XmlAccessorType(XmlAccessType.FIELD) public class Items { @XmlElement(name="Item") List<Item> items = new ArrayList(); } 

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

 @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; } 
+7
source share
5 answers

The error message says that you are getting an XML document that looks like this:

 <ItemSearchResponse> 

Instead of meeting the following namespace criteria:

 <ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01"> 
+6
source

Explanation here : A JAXBContext instance is initialized with the class (es) passed as a parameter and the classes that are statically accessible from these classes.

Initialize JAXBContext with the package so that it can see @XmlSchema declared in package-info.java:

 JAXBContext.newInstance("com.services.amazon") 
+4
source

If you use DocumentBuilderFactory in the getResponse method, try setting a namespace understanding:

 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); 

I had the same UnmarshalException and that solved it.

+2
source

Remove the namespace from package-info.java and change

 elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED 

to

 elementFormDefault = javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED. 
+1
source

Remove namespace from package-info.java This works for me

Example:

  @ javax.xml.bind.annotation.XmlSchema (namespace = "", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
0
source

All Articles