gpt4 book ai didi

java - 解析 XML 失败

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

调用 SOAP Web 服务后,我收到以下响应 XML 字符串。如何将响应解析回 Java 实体,以便可以相应地处理 ErrMsgStatus?我尝试使用下面的代码,但不断收到错误

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"NewDataSet"). Expected elements are <{TransactionalSubmissionsSvcs}NewDataSet>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726)

字符串中的示例响应

 <NewDataSet>
<SubmissionResult>
<Status>200</Status>
<RefNo>363180319bigKj83i</RefNo>
<ErrMsg>Successful</ErrMsg>
</SubmissionResult>
</NewDataSet>

示例客户端 XML 解析器

    JAXBContext jaxbContext = JAXBContext.newInstance(NewDataSet.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

StringReader reader = new StringReader("Sample Response in String above");
NewDataSet newDataSet = (NewDataSet) jaxbUnmarshaller.unmarshal(reader);
System.out.println(newDataSet.getSubmissionResult());

NewDataSet.java

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"submissionResult"
})
@XmlRootElement(name = "NewDataSet")
public class NewDataSet {

@XmlElement(name = "SubmissionResult", required = true)
protected NewDataSet.SubmissionResult submissionResult;

public NewDataSet.SubmissionResult getSubmissionResult() {
return submissionResult;
}

public void setSubmissionResult(NewDataSet.SubmissionResult value) {
this.submissionResult = value;
}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"status",
"refNo",
"errMsg"
})
public static class SubmissionResult {

@XmlElement(name = "Status")
protected short status;
@XmlElement(name = "RefNo", required = true)
protected String refNo;
@XmlElement(name = "ErrMsg", required = true)
protected String errMsg;

public short getStatus() {
return status;
}

public void setStatus(short value) {
this.status = value;
}

public String getRefNo() {
return refNo;
}

public void setRefNo(String value) {
this.refNo = value;
}

public String getErrMsg() {
return errMsg;
}

public void setErrMsg(String value) {
this.errMsg = value;
}
}

}

注意:如果可能的话,我更喜欢使用标准 java 库或定期维护且最新的外部库。

最佳答案

您的 POJO 模型中似乎有许多 NewDataSet 类。检查这一行代码 JAXBContext.newInstance(NewDataSet.class) 并检查 import 是否导入了正确的代码,因为异常表明编码器需要 TransactionalSubmissionsSvcs.NewDataSet >.

编辑前

你没有提到你使用什么工具。万一是Jackson您可以按如下方式解析 XML 有效负载:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

import java.io.File;
import java.nio.file.Files;

public class XmlMapperApp {

public static void main(String[] args) throws Exception {
String xml = "your xml";

parse(xml);
}

public static void parse(String xml) throws Exception {
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);

JsonNode root = xmlMapper.readTree(xml);
JsonNode result = root.at("/SendWithXMLResult");
JsonNode jsonNode = xmlMapper.readTree(result.asText());
JsonNode submissionResult = jsonNode.at("/SubmissionResult");

System.out.println(submissionResult.get("Status"));
System.out.println(submissionResult.get("ErrMsg"));
System.out.println(submissionResult.get("RefNo"));
}
}

如果您已准备好 POJO 模型,则可以使用 readValue 方法直接将 XML 反序列化为该模型。

关于java - 解析 XML 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55207297/

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