javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:""). Expected elements are
Below is some information that should help:
XML
Below is a portion of your XML. One thing to note is the xmlns
attribute. This is a special attribute and refers to namespace qualification within the XML document.
<lookupInstances xmlns='http://www.pqr.com/awd/rest/v1' name='LKIMGR'> <lookupParameters/></lookupInstances>
Below is another version of the XML with the same namespace qualification:
<abc:lookupInstances xmlns:abc='http://www.pqr.com/awd/rest/v1' name='LKIMGR'> <abc:lookupParameters/></abc:lookupInstances>
When you remove the xmlns
attribute the namespace qualification is removed. The document below is not equivalent to the two above.
<lookupInstances name='LKIMGR'> <lookupParameters/></lookupInstances>
Your Error
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.pqr.com/awd/rest/v1", local:"lookupInstances"). Expected elements are <{}lookupInstances>
This error indicates that you have not mapped the namespace qualification correctly.
Mapping the Namesapce Qualification in JAXB
Namespace qualification in JAXB is done using the package level @XmlSchema
annotation. Package level annotations go in package-info.java
. Below is the complete source for this class. If you already have a package-info.java
source file ensure it is being compiled and packaged with the rest of your classes.
@XmlSchema( namespace = "http://www.pqr.com/awd/rest/v1", elementFormDefault = XmlNsForm.QUALIFIED) package your_package;import javax.xml.bind.annotation.XmlNsForm;import javax.xml.bind.annotation.XmlSchema;
XML Schema
If you are generating your model from an XML Schema make sure this namespace qualification is properly define there. It will look like:
<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.pqr.com/awd/rest/v1" xmlns="http://www.pqr.com/awd/rest/v1" elementFormDefault="qualified"></xs:schema>
It seems that the class created by JAXB does not contain the namespace information:
@XmlType(name = "", propOrder = { "lookupParameters"})
I suspect that it causes this error, simply because it expects no namespace:
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.pqr.com/awd/rest/v1", local:"lookupInstances"). Expected elements are <{}lookupInstances>
I am not sure why the generated class lacks this information, but I would start with adding it manually to verify that this is the problem:
@XmlType(name = "", namespace= "http://www.pqr.com/awd/rest/v1", propOrder = { "lookupParameters"})