private static void replaceNodeWithString(Node node, Node nodeToReplace, Node replacementNode) {
NodeList nodeList = node.getChildNodes();
for(int i=0; i < nodeList.getLength(); i++){
Node childNode = nodeList.item(i);
if(childNode.getNodeName().equals(nodeToReplace.getNodeName())){
Element parentElement = (Element)childNode.getParentNode();
parentElement.insertBefore(replacementNode, childNode);
childNode.getParentNode().removeChild(childNode);
parentElement.normalize();
i--;
}
replaceNodeWithString(childNode, nodeToReplace, replacementNode);
}
}
Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/Ey5psAr7tR8/11939