gpt4 book ai didi

java - 将 Soap XML 响应转换为对象

转载 作者:行者123 更新时间:2023-12-01 17:00:28 28 4
gpt4 key购买 nike

我是 SOAP API 的新手

我有来自 API 的 SOAP 响应

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<LoginResponse xmlns="http://test.org/ADMail_Service">
<LoginResult>
<ErrorMessage>Successful login</ErrorMessage>
<Status>true</Status>
</LoginResult>
</LoginResponse>
</soapenv:Body>
</soapenv:Envelope>

我正在尝试将其转换为一个对象。

通过在线阅读文章,我尝试使用 JAXB 来执行此操作,但我的对象是空的。

这是读取响应的代码。我将响应写入 xml 文件以进行测试:

try {
XMLInputFactory xif = XMLInputFactory.newFactory();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
xsr.nextTag(); // Advance to Envelope tag

xsr.nextTag(); // Advance to Body tag
xsr.nextTag();
xsr.nextTag();


JAXBContext jc = JAXBContext.newInstance(LoginResult.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<LoginResult> je = unmarshaller.unmarshal(xsr, LoginResult.class);

System.out.println(je.getName());
System.out.println(je.getValue());
} catch (XMLStreamException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}

LoginResult 类:

public class LoginResult {
private String errorMessage;
private String status;

public String getErrorMessage() {
return errorMessage;
}

public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}

public String getStatus() {
return status;
}

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

提前致谢!

最佳答案

您可以使用此代码检索 POJO,还可以将 @XmlRootElement 作为 header 添加到 POJO。

(我没有测试下面的代码)

XMLInputFactory xif = XMLInputFactory.newFactory();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
xsr.nextTag(); // Advance to Envelope tag

xsr.nextTag(); // Advance to Body tag
xsr.nextTag();
xsr.nextTag();

Transformer transformer = TransformerFactory.newInstance().newTransformer();
StringWriter stringWriter = new StringWriter();
transformer.transform(new StAXSource(xsr), new StreamResult(stringWriter));
StringReader sr = new StringReader(stringWriter.toString());
JAXBContext jaxbContext = JAXBContext.newInstance(LoginResult.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
LoginResult loginResult = (LoginResult) unmarshaller.unmarshal(sr);

编辑:

我为您找到了解决方案:

    @XmlRootElement(name = "LoginResult", namespace = "http://test.org/ADMail_Service")
@XmlAccessorType(XmlAccessType.FIELD)
public class LoginResult {
@XmlElement(name = "ErrorMessage", namespace = "http://test.org/ADMail_Service")
private String errorMessage;
@XmlElement(name = "Status", namespace = "http://test.org/ADMail_Service")
private String status;

public String getErrorMessage() {
return errorMessage;
}

public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}

public String getStatus() {
return status;
}

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


public static void main(String[] args) {
try {
XMLInputFactory xif = XMLInputFactory.newFactory();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
xsr.nextTag(); // Advance to Envelope tag

xsr.nextTag(); // Advance to Body tag
xsr.nextTag();
xsr.nextTag();


JAXBContext jc = JAXBContext.newInstance(LoginResult.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<LoginResult> je = unmarshaller.unmarshal(xsr, LoginResult.class);

System.out.println(je.getName());
System.out.println(je.getValue());
System.out.println(je.getValue().getErrorMessage());
} catch (XMLStreamException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}

}

关于java - 将 Soap XML 响应转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61514461/

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