javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:""). Expected elements are
Solution 1
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>
Solution 2
Change
@XmlRootElement(name = "lookupInstances")
to
@XmlRootElement( namespace = "http://www.pqr.com/awd/rest/v1", name = "lookupInstances")
Solution 3
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"
})
Solution 4
I have changed the jaxb class by changing the xsd and it is working now.
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2015.01.13 at 03:49:52 PM IST
//
package com.dsths.ga.awd.main;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="lookupParameters">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="lookupParameter" maxOccurs="unbounded" minOccurs="0">
* <complexType>
* <simpleContent>
* <extension base="<http://www.w3.org/2001/XMLSchema>string">
* <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
* </extension>
* </simpleContent>
* </complexType>
* </element>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </element>
* </sequence>
* <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"lookupParameters"
})
@XmlRootElement(name = "lookupInstances")
public class LookupInstances {
@XmlElement(required = true)
protected LookupInstances.LookupParameters lookupParameters;
@XmlAttribute(name = "name")
protected String name;
/**
* Gets the value of the lookupParameters property.
*
* @return
* possible object is
* {@link LookupInstances.LookupParameters }
*
*/
public LookupInstances.LookupParameters getLookupParameters() {
return lookupParameters;
}
/**
* Sets the value of the lookupParameters property.
*
* @param value
* allowed object is
* {@link LookupInstances.LookupParameters }
*
*/
public void setLookupParameters(LookupInstances.LookupParameters value) {
this.lookupParameters = value;
}
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="lookupParameter" maxOccurs="unbounded" minOccurs="0">
* <complexType>
* <simpleContent>
* <extension base="<http://www.w3.org/2001/XMLSchema>string">
* <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
* </extension>
* </simpleContent>
* </complexType>
* </element>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"lookupParameter"
})
public static class LookupParameters {
protected List<LookupInstances.LookupParameters.LookupParameter> lookupParameter;
/**
* Gets the value of the lookupParameter property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the lookupParameter property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getLookupParameter().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link LookupInstances.LookupParameters.LookupParameter }
*
*
*/
public List<LookupInstances.LookupParameters.LookupParameter> getLookupParameter() {
if (lookupParameter == null) {
lookupParameter = new ArrayList<LookupInstances.LookupParameters.LookupParameter>();
}
return this.lookupParameter;
}
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <simpleContent>
* <extension base="<http://www.w3.org/2001/XMLSchema>string">
* <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
* </extension>
* </simpleContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"value"
})
public static class LookupParameter {
@XmlValue
protected String value;
@XmlAttribute(name = "name")
protected String name;
/**
* Gets the value of the value property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getValue() {
return value;
}
/**
* Sets the value of the value property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setValue(String value) {
this.value = value;
}
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
}
}
}
Rana
Updated on July 05, 2022Comments
-
Rana almost 2 years
You can find a lot of questions like this in this site but none has solved my issue.
This is my XML :
<?xml version='1.0' encoding='UTF-8' standalone='yes'?> <lookupInstances xmlns='http://www.pqr.com/awd/rest/v1' name='LKIMGR'> <lookupParameters> <lookupParameter name='businessArea'>PQAA</lookupParameter> <lookupParameter name='MEMBERNUMBER'>ANTHONY1900</lookupParameter> </lookupParameters> </lookupInstances>
I have created the Jaxb class using the XJC compiler and the file created is as :
// // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2015.01.13 at 12:15:41 PM IST // package com.dsths.ga.awd.main; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; /** * <p>Java class for anonymous complex type. * * <p>The following schema fragment specifies the expected content contained within this class. * * <pre> * <complexType> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element name="lookupParameters"> * <complexType> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element name="lookupParameter"> * <complexType> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" /> * </restriction> * </complexContent> * </complexType> * </element> * </sequence> * </restriction> * </complexContent> * </complexType> * </element> * </sequence> * <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" /> * </restriction> * </complexContent> * </complexType> * </pre> * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "lookupParameters" }) @XmlRootElement(name = "lookupInstances") public class LookupInstances { @XmlElement(required = true) protected LookupInstances.LookupParameters lookupParameters; @XmlAttribute protected String name; /** * Gets the value of the lookupParameters property. * * @return * possible object is * {@link LookupInstances.LookupParameters } * */ public LookupInstances.LookupParameters getLookupParameters() { return lookupParameters; } /** * Sets the value of the lookupParameters property. * * @param value * allowed object is * {@link LookupInstances.LookupParameters } * */ public void setLookupParameters(LookupInstances.LookupParameters value) { this.lookupParameters = value; } /** * Gets the value of the name property. * * @return * possible object is * {@link String } * */ public String getName() { return name; } /** * Sets the value of the name property. * * @param value * allowed object is * {@link String } * */ public void setName(String value) { this.name = value; } /** * <p>Java class for anonymous complex type. * * <p>The following schema fragment specifies the expected content contained within this class. * * <pre> * <complexType> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element name="lookupParameter"> * <complexType> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" /> * </restriction> * </complexContent> * </complexType> * </element> * </sequence> * </restriction> * </complexContent> * </complexType> * </pre> * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "lookupParameter" }) public static class LookupParameters { @XmlElement(required = true) protected LookupInstances.LookupParameters.LookupParameter lookupParameter; /** * Gets the value of the lookupParameter property. * * @return * possible object is * {@link LookupInstances.LookupParameters.LookupParameter } * */ public LookupInstances.LookupParameters.LookupParameter getLookupParameter() { return lookupParameter; } /** * Sets the value of the lookupParameter property. * * @param value * allowed object is * {@link LookupInstances.LookupParameters.LookupParameter } * */ public void setLookupParameter(LookupInstances.LookupParameters.LookupParameter value) { this.lookupParameter = value; } /** * <p>Java class for anonymous complex type. * * <p>The following schema fragment specifies the expected content contained within this class. * * <pre> * <complexType> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" /> * </restriction> * </complexContent> * </complexType> * </pre> * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "") public static class LookupParameter { @XmlAttribute protected String name; /** * Gets the value of the name property. * * @return * possible object is * {@link String } * */ public String getName() { return name; } /** * Sets the value of the name property. * * @param value * allowed object is * {@link String } * */ public void setName(String value) { this.name = value; } } } }
Unmarshal Code :
public LookupInstances unmarshallXmlRequest(String xmlReq) { LookupInstances instances = null; try { JAXBContext jc = JAXBContext.newInstance( LookupInstances.class ); Unmarshaller u = jc.createUnmarshaller(); StringBuffer xmlStr = new StringBuffer( xmlReq ); StringReader strReader = new StringReader( xmlStr.toString() ); StreamSource strSource = new StreamSource(strReader); Object o = u.unmarshal( strSource ); instances = (LookupInstances)o; } catch (JAXBException e) { e.printStackTrace(); } return instances; }
I got this error :
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.pqr.com/awd/rest/v1", local:"lookupInstances"). Expected elements are <{}lookupInstances>
Please help.
-
Rana over 9 yearsThanks for a quick reply @Magnilex. As you suggested I added the namespace in java class declaration and the error vanished. But while unmarshaling the java class, it does not contain all elements or not properly initialised. I changed the jaxb class which now contains List to capture lookupParameter and It is working now.
-
Magnilex over 9 years@Rana Glad to help, and good that my answer pushed you in the right direction.
-
Rana over 9 yearsThat helped a lot Blaise. I was wondering how the app is working without mentioning namespace qualification in Java class, but as you explained it was package-info.java which was having that info. In my first attempt I got the xsd file wrong which dragged me into an incorrect Jaxb class. Thanks a lot.
-
faizi over 7 yearsIs there any way to supply the xmlns URI from a properties file ? For me the xmlns URI need to change dynamically for test and production environments
-
ClickBright over 6 years@XmlSchema(namespace = "pqr.com/awd/rest/v1", elementFormDefault = XmlNsForm.QUALIFIED) package your_package; import javax.xml.bind.annotation.XmlNsForm; import javax.xml.bind.annotation.XmlSchema; This part is very important for JAXB generated classes, especially
elementFormDefault = XmlNsForm.QUALIFIED
will not automatically be added by the JAXB. Thanks Blaise, your answer helped me.