Reputation: 1078
I am getting the following error when trying to convert xml response to java objects using jaxb
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://SOMETHING/doc/2006-03-01/", local:"Name"). Expected elements are <{}Name>,<{}IsTruncated>,<{}MaxKeys>,<{}Contents>,<{}Prefix>,<{}Marker>
Here is my XML :
<ListBucketResult xmlns="http://something/doc/2006-03-01/">
<Name>test2</Name>
<Prefix/>
<Marker/>
<MaxKeys>3</MaxKeys>
<IsTruncated>false</IsTruncated>
<Contents>
<Key>metadata.xml</Key>
<LastModified>2012-09-04T08:29:36.000Z</LastModified>
<ETag>6b836fd43c402681506926b2248ec418</ETag>
<Size>3258</Size>
<StorageClass>STANDARD</StorageClass>
</Contents>
</ListBucketResult>
And my java object classes are something like this
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"name",
"prefix",
"marker",
"maxKeys",
"isTruncated",
"contents"
})
@XmlRootElement(name = "ListBucketResult")
public class ListBucketResult {
@XmlElement(name = "Name", required = true)
protected String name;
@XmlElement(name = "Prefix", required = true)
protected String prefix;
@XmlElement(name = "Marker", required = true)
protected String marker;
@XmlElement(name = "MaxKeys")
protected int maxKeys;
@XmlElement(name = "IsTruncated")
protected boolean isTruncated;
@XmlElement(name = "Contents", required = true)
protected ListBucketResult.Contents contents;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"key",
"lastModified",
"eTag",
"size",
"storageClass"
})
public static class Contents {
@XmlElement(name = "Key", required = true)
protected String key;
@XmlElement(name = "LastModified", required = true)
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar lastModified;
@XmlElement(name = "ETag", required = true)
protected String eTag;
@XmlElement(name = "Size")
protected int size;
@XmlElement(name = "StorageClass", required = true)
protected String storageClass;
and finally my unmarshalling code is :
JAXBContext jc = JAXBContext.newInstance(ListBucketResult.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
JAXBElement element = (JAXBElement) unmarshaller.unmarshal (inputStream);
ListBucketResult customer = (ListBucketResult) element.getValue();
Could someone please let me know what am i doing incorrect ?
Upvotes: 6
Views: 34669
Reputation: 9
I was getting similar error org.codehaus.jackson.JsonParseException and javax.xml.bind.UnmarshalException : with linked exception: [javax.xml.bind.UnmarshalException: unexpected element (uri:"sometext", local:"elementA"). Expected elements are .
This is when integrating RESTEasy and Spring with my DTO's generated using JAXB. I was using Jackson for conversion.
To solve it I introduced the Jackson library in the Maven dependency and it solved my problem. The jackson dependency that is specific to this was
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-xc</artifactId>
<version>1.9.9</version>
</dependency>
There were other Jackson jars as well which I needed, so my POM Essentially looked as below.
//The jackson-core-asl jar contains streaming JSON parser and generator interfaces and implementations
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.9</version>
</dependency>
//Ability to use JAXB annotations containing classes needed to add XML compatibility support.
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-xc</artifactId>
<version>1.9.9</version>
</dependency>
//Mapper jar contains functionality for data binding:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.9</version>
</dependency> //This dependency makes a JAX-RS implementation like Jersey,RESTEasy use Jackson for binding of JSON to-from Java objects
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.9</version>
</dependency>
Upvotes: 0
Reputation: 149017
You can use the @XmlSchema
annotation on a package-info
class to control the namespace qualification. If you have already written a package-info class make sure it is being compiled (some versions of ant had problems with package-info classes).
package-info
@XmlSchema(
namespace = "http://something/doc/2006-03-01/",
elementFormDefault = XmlNsForm.QUALIFIED)
package example;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
For More Information
Upvotes: 5
Reputation: 864
The XML document contains elements which are part of the 'http://something/doc/2006-03-01/' namespace, but the annotated java class is not associated with that namespace. Try changing the @XmlType annotation to:
@XmlType(name = "", namespace="http://something/doc/2006-03-01/", propOrder = { ...
Upvotes: 3