gpt4 book ai didi

java - 无法将 xml 字符串解码为 java 对象

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

将 xml 字符串解码为 java 对象会出现以下错误:

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"AppServerResponse"). Expected elements are <{http://www.coda.com/efinance/schemas/appserver}AppServerResponse>

这是 AppServerResponse Java 类

@XmlRootElement (name = "AppServerResponse", namespace = "http://www.coda.com/efinance/schemas/appserver")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "AppServerResponse", namespace = "http://www.coda.com/efinance/schemas/appserver", propOrder = {
"getEnvironment",
"listUserCompanies",
"softToHardDate"
})
public class AppServerResponse extends ServiceResponse
{

@XmlElement(name = "GetEnvironment", namespace = "http://www.coda.com/efinance/schemas/appserver")

protected GetEnvironmentResponseVerb getEnvironment;
@XmlElement(name = "ListUserCompanies", namespace = "http://www.coda.com/efinance/schemas/appserver")
protected ListUserCompaniesResponseVerb listUserCompanies;
@XmlElement(name = "SoftToHardDate", namespace = "http://www.coda.com/efinance/schemas/appserver")

protected SoftToHardDateResponseVerb softToHardDate;

解码函数的代码和已解码的 xml 消息:

String strResponse = "<AppServerResponse uri=\"http://www.coda.com/efinance/schemas/appserver\" version=\"2.0\" >"
+ "<GetEnvironment>"
+ "<Response status=\"success\">"
+ "<Environment>"
+ "<CmpCode>TEST</CmpCode>"
+ "<CapCode>INTERFACE</CapCode>"
+ "<HomeCurr>EUR</HomeCurr>"
+ "<DateOrder>1</DateOrder>"
+ "<DateDisplay>1</DateDisplay>"
+ "<DateSep>/</DateSep>"
+ "<HomeCurrDps>2</HomeCurrDps>"
+ "<HomeCurrSymbol>€</HomeCurrSymbol>"
+ "<HomeCurrSymbolPos>before</HomeCurrSymbolPos>"
+ "<HomeCurrLinkType>no_link</HomeCurrLinkType>"
+ "<HomeCurrParent>EURO</HomeCurrParent>"
+ "<CurrentPeriod>2018/4</CurrentPeriod>"
+ "<CmpTimeStamp>11</CmpTimeStamp>"
+ "<CapTimeStamp>3</CapTimeStamp>"
+ "<UsrTimeStamp>12</UsrTimeStamp>"
+ "<ServerVersion>14.000.0014</ServerVersion>"
+ "</Environment>"
+ "</Response>"
+ "</GetEnvironment>"
+ "</AppServerResponse>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(false);
DocumentBuilder db;

db = dbf.newDocumentBuilder();

Document docResponse = db.parse(new InputSource(new StringReader(strResponse)));

writeXmlDocumentToXmlFile(docResponse);

JAXBContext contextResponse = JAXBContext.newInstance(responseClass);
Unmarshaller u = contextResponse.createUnmarshaller();

returnValue = u.unmarshal(docResponse);

最佳答案

您的 JAXB 注释指定您的元素位于 http://www.coda.com/efinance/schemas/appserver 命名空间中,但响应字符串中的元素实际上不在命名空间中。看来您打算通过设置默认命名空间

String strResponse = "<AppServerResponse uri=\"http://www.coda.com/efinance/schemas/appserver\"..."

但是 uri 属性实际上并没有做到这一点。相反,您需要 xmlns 属性:

String strResponse = "<AppServerResponse xmlns=\"http://www.coda.com/efinance/schemas/appserver\"..."

请注意 xmlns 属性名称而不是 uri

关于java - 无法将 xml 字符串解码为 java 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57379152/

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