将XML字符串转换为SOAPBody的正确方法是什么。以下是我的实施
InputStream is = new ByteArrayInputStream(xmlResponse.getBytes());
SOAPMessage message = MessageFactory.newInstance().createMessage(null, is);
SOAPBody body = message.getSOAPPart().getEnvelope().getBody();
我得到了[SOAP-ENV:Body:null]。 xmlResponse是方法的输入参数。
答案 0 :(得分:0)
首先,要在SOAPBody上设置正文xml,您需要提供xml(信封节点等)的完整结构,然后只需要删除标头节点及其节点子节点。
第二,您将主体xml设置在错误的图层中。贝娄举了一个例子:
//This needs be your body xml that you want to set on SOAPBody
InputStream is = new ByteArrayInputStream(xmlResponse.getBytes());
//convert inputStream to Source
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware(true);
DocumentBuilder builder = dbFactory.newDocumentBuilder();
Document document = builder.parse(is);
DOMSource domSource = new DOMSource(document);
//create new SOAPMessage instance
MessageFactory mf = MessageFactory.newInstance(javax.xml.soap.SOAPConstants.SOAP_1_1_PROTOCOL);
SOAPMessage message = mf.createMessage();
SOAPPart part = message.getSOAPPart();
//Set the xml in SOAPPart
part.setContent(domSource);
message.saveChanges();
//access the body
SOAPBody body = message.getSOAPPart().getEnvelope().getBody();
//for you to access the values inside body, you need to access the node childs following the structures.
//example
body.getFirstChild();
我认为这可以解决您的问题。