Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将XML字符串转换为SOAPBody_Java_Xml_Web Services_Soap_Soap Client - Fatal编程技术网

Java 将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

将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是该方法的输入参数

首先,要在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