Java 将XML字符串转换为SOAPBody
将XML字符串转换为SOAPBody的正确方法是什么。下面是我的实现Java 将XML字符串转换为SOAPBody,java,xml,web-services,soap,soap-client,Java,Xml,Web Services,Soap,Soap Client,将XML字符串转换为SOAPBody的正确方法是什么。下面是我的实现 InputStream is = new ByteArrayInputStream(xmlResponse.getBytes()); SOAPMessage message = MessageFactory.newInstance().createMessage(null, is); SOAPBody body = message.getSOAPPart().getEnvelope().getBody(); 我得到了[SOA
InputStream is = new ByteArrayInputStream(xmlResponse.getBytes());
SOAPMessage message = MessageFactory.newInstance().createMessage(null, is);
SOAPBody body = message.getSOAPPart().getEnvelope().getBody();
我得到了[SOAP-ENV:Body:null]。xmlResponse是该方法的输入参数 首先,要在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();
我想这会解决你的问题。你只是想得到身体元素吗?或者整个XML?不,我不想获取body元素,只需要将XML转换为SOAPBody