解组XML时出现javax.xml.bind.UnmarshalException

时间:2017-07-25 14:30:46

标签: java xml jaxb

我使用以下代码使用JAXB解组XML。 responseXML包含从Web服务调用返回的XML字符串。

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

以下是解组时发生的异常。

 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: ]

有人帮助解决这个问题。

下面的

是从XSD自动生成的TestResponse类

@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;
    }


}

这是示例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>

1 个答案:

答案 0 :(得分:1)

我认为XML中的命名空间导致解析问题。尝试在package-info.java中指定名称空间,并在其中提及前缀为“a”。您可以查看更多信息here

您可以查看类似的问题here