gpt4 book ai didi

java - 将字符串 XML 转换为 SoapMessage

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

我正在尝试使用下面的代码将字符串 xml 转换为soapMessage,

 System.out.println("response:" + response.toString());
InputStream is = new ByteArrayInputStream(response.toString().getBytes());
SOAPMessage responseSoap = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(null, is);
System.out.println("body "+responseSoap.getSOAPBody());
System.out.println("1");
QName bodyName = new QName("Response");
SOAPBody sb = responseSoap.getSOAPBody();
System.out.println("2");
Iterator iterator = sb.getChildElements(bodyName);
System.out.println("entered into SoapResponse 3");
while (iterator.hasNext()) {
System.out.println("entered into SoapResponse 4");
SOAPBodyElement bodyElement = (SOAPBodyElement) iterator.next();
String val = bodyElement.getValue();
System.out.println("The Value is:" + val);
}

打印,

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><Response xmlns="http://service.com/integration/"><Result xsi:type="LoginResponse"><UserId>12</UserId><TypeId>1</TypeId><Success>true</Success></Result></Response></soap:Body></soap:Envelope>

body [soap:Body: null]

为什么我的 body 为 null?

最佳答案

toString()实现SoapBody返回"["+getNodeName()+": "+getNodeValue()+"]"; 。这是在 NodeImpl.java 中实现的这是 SoapBody 的实现超一流Node 。在你的情况下 getNodeValue()为空。

如果您的要求是打印响应,您可以执行 responseSoap.writeTo(System.out); .

您可能还想替换

QName bodyName = new QName("Response");

QName bodyName = new QName("http://service.com/integration/", "Response");

也可以使用String val = bodyElement.getTextContent();如果它对您的用例有意义。 getValue用于文本节点。

关于java - 将字符串 XML 转换为 SoapMessage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55293414/

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