【发布时间】: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 自动生成的文件。