sher17
sher17

Reputation: 617

javax.xml.bind.UnmarshalException iccurs when unmarshalling an XML

I use the below code to unmarshal an XML using JAXB. responseXML contains the XML string returned from a web service call.

    StringReader reader = new StringReader(responseXML);
    jaxbContext = JAXBContext.newInstance(TestResponse.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    Object idResponse = unmarshaller.unmarshal(reader);

Below is the Exception which occurs when unmarshalling.

 javax.xml.bind.UnmarshalException
  - with linked exception:
 [Exception [EclipseLink-25004] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.XMLMarshalException
 Exception Description: An error occurred unmarshalling the document
 Internal Exception: java.lang.IllegalArgumentException: ]

Someone help to work on this.

below is the TestResponse class which is auto generated from an XSD

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "responseCode",
    "responseDescription",
    "responsemessage"
})
@XmlRootElement(name = "TestResponse")
public class TestResponse {

    @XmlElement(required = true)
    protected String responseCode;
    @XmlElement(required = true)
    protected String responseDescription;
    protected String responsemessage;


    /**
     * Gets the value of the responseCode property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getResponseCode() {
        return responseCode;
    }

    /**
     * Sets the value of the responseCode property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setResponseCode(String value) {
        this.responseCode = value;
    }

    /**
     * Gets the value of the responseDescription property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getResponseDescription() {
        return responseDescription;
    }

    /**
     * Sets the value of the responseDescription property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setResponseDescription(String value) {
        this.responseDescription = value;
    }

    /**
     * Gets the value of the responsemessage property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getResponsemessage() {
        return responsemessage;
    }

    /**
     * Sets the value of the responsemessage property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setResponsemessage(String value) {
        this.responsemessage = value;
    }


}

here is the sample XML

<a:TestResponse xmlns:a="http://esm.mtn.co.za/data/commonbusiness/TestValidate">
    <a:responseCode>0</a:responseCode>
    <a:responseDescription>Success</a:responseDescription>
    <a:responsemessage>Success</a:responsemessage>  
</a:TestResponse>

Upvotes: 0

Views: 2240

Answers (1)

Arpit
Arpit

Reputation: 370

I think the namespace in the XML is causing a problem for parsing. Try to specify the namespace in package-info.java and mention the prefix as "a" in there. You can check more information here.

You could check a similar question here

Upvotes: 1

Related Questions