XPath和SOAP Body节点替换为字符串

时间:2018-03-20 20:20:45

标签: java xml string xpath soap

我试图用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的形式返回,所以它没有用字符串替换

0 个答案:

没有答案