12

I am getting an exception while turning an XML response from a service to a POJO. The XML looks like this:

Here is my XML response.

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

@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; } 

5 Answers 5

6

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

<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"> 
Sign up to request clarification or add additional context in comments.

2 Comments

hmmm but my xml does have a namespace qualification... as I posted. but you are right, if I remove the namespace from package-info.java then it works fine.
@Ethan - Is the url parameter in your code really set to gist.githubusercontent.com/Omnipresent/9513522/raw/… or to something else?
4

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

Initialize JAXBContext using package, so it can see @XmlSchema declared in package-info.java:

JAXBContext.newInstance("com.services.amazon") 

2 Comments

It isn't required to bootstrap the JAXBContext on the package name to get the @XmlSchema annotation. Currently according to the error message the JAXB is expecting the namespace, it is the XML that appears to be missing it.
Can be, then author should check the input, the one that he provided does have namespace
3

If you're using DocumentBuilderFactory in your getResponse method, try setting namespace awareness:

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

I had the same UnmarshalException and this solved it.

Comments

1

Remove the namespace from package-info.java and change

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

to

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

Comments

0

Remove the namespace from package-info.java It's work for me

Ex:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.