我正在尝试使用以下代码将字符串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]
为什么我的身体为空?
答案 0 :(得分:1)
toString()
的{{1}}实现返回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();
用于文本节点。