7

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 reason it doesn't.

File xmlFile = new File("c:\\file.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();

NodeList nodes = doc.getElementsByTagName("NodeToReplace");
for (int i = 0; i < nodes.getLength(); i++) {

    NodeList children = nodes.item(i).getChildNodes();
    for (int j = 0; j < children.getLength(); j++) {
          nodes.item(i).removeChild(children.item(j));
    }
        doc.renameNode(nodes.item(i), null, "MyCustomTag");  
}

EDIT-

After debugging it for a while, I sovled it. The problem was in moving index of the children array elmts. Here's the code:

NodeList nodes = doc.getElementsByTagName("NodeToReplace");
for (int i = 0; i < nodes.getLength(); i++) {

    NodeList children = nodes.item(i).getChildNodes();

    int len = children.getLength();
    for (int j = len-1; j >= 0; j--) {
        nodes.item(i).removeChild((Node) children.item(j));
    }
    doc.renameNode(nodes.item(i), null, "MyCustomTag");  
}
4
  • 1
    "for an unknown reason it doesn't" is not helpful. What does it do? Nothing at all? Throws an Exception? Makes egg and bacon? Commented Oct 26, 2011 at 14:24
  • Do you have to manually remove the children? Is it not possible to just replace the node with the one you want to have? Commented Oct 26, 2011 at 14:25
  • It renames the node, but it fails in deleting it's childern and I can't find the bug in it. Commented Oct 26, 2011 at 14:26
  • He's renaming the node rather than deleting and creating a new one, I presume that's why he's trying to remove children as well. Are you doing it this way because you want to make sure they are in exactly the same place? Commented Oct 26, 2011 at 14:27

2 Answers 2

12

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

There are a few ways, you'd probably want to use doc.createTextNode(String) to create a template node and then clone that for each newNode.
-3

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

1 Comment

structural string manipulation should be avoided with XML because it ignores things like hierarchy, context, and other semantics that xml enforces. assuming an XML file is a flat string is sure to create more headaches than it is worth

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.