I'm trying to set a node value via an XPath. I have the following but it doesn't seem to change the actual files value.
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
xPathExpression = "//test";
xPathValue= "111";
NodeList nodes = (NodeList) xPath.evaluate(xPathExpression, new InputSource(new FileReader(fileName)), XPathConstants.NODESET);
for (int k = 0; i < nodes.getLength(); i++)
{
System.out.println(nodes.item(k).getTextContent()); // Prints original value
nodes.item(k).setTextContent(xPathValue);
System.out.println(nodes.item(k).getTextContent()); // Prints 111 after
}
But file contents for that node remain unchanged.
How do I set the value of that node?
Thanks