gpt4 book ai didi

解码 XML 时发生 javax.xml.bind.UnmarshalException

转载 作者:行者123 更新时间:2023-12-02 12:30:10 26 4
gpt4 key购买 nike

我使用以下代码通过 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 中的命名空间导致了解析问题。尝试在 package-info.java 中指定 namespace ,并在其中将前缀指定为“a”。您可以查看更多信息here

您可以检查类似的问题 here

关于解码 XML 时发生 javax.xml.bind.UnmarshalException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45306333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com