I have a SOAP message as as string which looks like:
String testMsg = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:wsa=\"http://www.w3.org/2005/08/addressing\"><soap:Header>Some Header stuff</soap:Header><soap:Body>Some Body Stuff</soap:Body></soap:Envelope>";
Then it goes to XPath but can't find a node (came as null) to set text value
Document doc = convertStringToDocument(testMsg);
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 was removed");
I try to convert it to XML document using method below
private static Document convertStringToDocument(String xmlStr) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
try
{
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xmlStr));
Document doc = builder.parse(is);
return doc;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Any thoughts?