Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
}
like image 773
Ethan Avatar asked Mar 12 '14 18:03

Ethan


People also ask

What is UnmarshalException?

public class UnmarshalException extends JAXBException. This exception indicates that an error has occurred while performing an unmarshal operation that prevents the JAXB Provider from completing the operation. The ValidationEventHandler can cause this exception to be thrown during the unmarshal operations.

What is JAXBContext newInstance?

JAXBContext.newInstance( "com.acme.foo:com.acme.bar" ) The JAXBContext instance is initialized from a list of colon separated Java package names. Each java package contains JAXB mapped classes, schema-derived classes and/or user annotated classes.

What is JAXB in Java?

Java Architecture for XML Binding (JAXB) provides a fast and convenient way to bind XML schemas and Java representations, making it easy for Java developers to incorporate XML data and processing functions in Java applications.

What is XmlAccessorType?

Annotation Type XmlAccessorType Controls whether fields or Javabean properties are serialized by default. Usage. @XmlAccessorType annotation can be used with the following program elements: package. a top level class.


4 Answers

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">
like image 147
bdoughan Avatar answered Oct 05 '22 16:10

bdoughan


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")
like image 30
hoaz Avatar answered Oct 05 '22 17:10

hoaz


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.

like image 38
dmarwick Avatar answered Oct 05 '22 15:10

dmarwick


Remove the namespace from package-info.java and change

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

to

elementFormDefault = javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED.
like image 33
Stack overflow user Avatar answered Oct 05 '22 17:10

Stack overflow user