What is the most efficient way to iterate through all DOM elements in Java?
Something like this but for every single DOM elements on current org.w3c.dom.Document?
for(Node childNode = node.getFirstChild(); childNode!=null;){
Node nextChild = childNode.getNextSibling();
// Do something with childNode, including move or delete...
childNode = nextChild;
}
Basically you have two ways to iterate over all elements:
1. Using recursion (the most common way I think):
2. Avoiding recursion using
getElementsByTagName()method with*as parameter:I think these ways are both efficient.