Tag Archives: w3c

The Java DOM API sucks

How it sucks
I recently wrote some code to filter nodes from a DOM and had issues with NullPointerExceptions:
NodeList nl = doc.getElementsByTagName(“mytag”);
for(int i = 0, n = nl.getLength(); i < n; i++) {
Element elt = (Element) nl.item(i);
if(…) {
elt.getParentNode().removeChild(elt);
}
}
The problem was that NodeLists are live, and removing elements while iterating changes the indexes. When I tried to [...]