【问题标题】:javax.xml.bind.UnmarshalException iccurs when unmarshalling an XML解组 XML 时 javax.xml.bind.UnmarshalException iccurs
【发布时间】:2017-12-31 13:25:06
【问题描述】:

我使用以下代码使用 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>

【问题讨论】:

  • 请发布您的 XML 和相应的类 (TestResponse)。
  • 您在responsemessage 字段中缺少@XmlElement 注释?
  • @XmlElement(required = true) 注释仅在字段为必填时添加。这是从 xsd 自动生成的文件。

标签: java xml jaxb


【解决方案1】:

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

您可以查看类似的问题here

【讨论】:

  • 从我的 Web 服务调用返回的响应 XML 有一些问题,导致解组时出现异常。当我从 XSD 文件中删除一些未使用的部分时,它起作用了。
  • 太棒了!不过,您可以使用 minOccurs 属性将 XSD 中可选的部分标记为可选(也就是说,如果这些部分可以再次出现,在其他一些响应中最好将它们标记为可选而不是删除(我知道,这不是你要求))。
  • 由于我自动生成 XSD,可选字段会被自动标记,因此它们也会在生成的 java 文件中进行注释。将未使用的字段注释为这对解决问题没有任何影响。我在我的项目中使用 eclipselink。我怀疑这可能是其中一个原因。
猜你喜欢
  • 1970-01-01
  • 2013-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-09
  • 1970-01-01
相关资源
最近更新 更多