I wan to replace a node in XML document with another and as a consequence replace all it\'s children with other content. Following code should work, but for an unknown reaso
Try using replaceChild to do the whole hierarchy at once:
NodeList nodes = doc.getElementsByTagName("NodeToReplace");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
Node newNode = // Create your new node here.
node.getParentNode().replaceChild(newNode, node);
}
Easy way to do is using regular expression.
String payload= payload.replaceAll("<payload>([^<]*)</payload>", "<payload>NODATA</payload>");
This will make sure all the payload nodes contents are replaced with NODATA