我试图用SOAP消息(SOAPMessage对象)中的字符串替换SOAP Body元素,该字符串看起来像
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://www.w3.org/2005/08/addressing">
<soap:Header>
..... SOAP Headers ....
</soap:Header>
<soap:Body>
... SOAP body part...
</soap:Body>
</soap:Envelope>
到
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://www.w3.org/2005/08/addressing">
<soap:Header>
..... SOAP Headers ....
</soap:Header>
<soap:Body>
Body part was removed.
</soap:Body>
</soap:Envelope>
为此,我使用了XPath
Document doc = convertStringToDocument(message);
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "//soap:Envelope/soap:Body";
Node node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
// Set the node content
node.setTextContent("Body part was removed.");
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
StringWriter buffer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(buffer));
String markedMessage = buffer.toString();
但是它以node = null
的形式返回,所以它没有用字符串替换