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.
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.
xml. bind. Provides a runtime binding framework for client applications including unmarshalling, marshalling, and validation capabilities.
Maps a JavaBean property to a XML element derived from property name. Usage. @XmlElement annotation can be used with the following program elements: a JavaBean property. non static, non transient field.
public class JAXBElement<T> extends Object implements Serializable. JAXB representation of an Xml Element. This class represents information about an Xml Element from both the element declaration within a schema and the element instance value within an xml document with the following properties. element's xml tag name.
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>
Change
@XmlRootElement(name = "lookupInstances")
to
@XmlRootElement( namespace = "http://www.pqr.com/awd/rest/v1", name = "lookupInstances")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With