I'm looking for a way to write a search and replace method using W3C DOM to update a tree. I've thought about doing a breadth-first search as below, but can't think of how to do the in place replacement?
import org.w3c.dom.Node;
private Element tree;
public void searchReplace(Node x, Node y){
Queue<Node> q = new LinkedList<Node>();
q.add(tree);
while (!q.isEmpty()) {
Node current = q.remove();
if (current == x){
// do replacement
}
NodeList children = current.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
q.add(children.item(i));
}
}
}
I'm only interested in replacing the node name not its value.