I was wondering if any Java/XML gurus could help me out here. I wish to remove a subset of <item> elements from a Document, using code similar to:
However, in removing a child, I believe the NodeList gets updated, so that [tt]items.item(i)[/tt] skips over an item that has been shifted down? I made a simple filter which always returns false, and this produced a Document where each alternate <item> element was successfully removed. How should I approach removing these elements?
Thanks for any advice, Neil
Code:
public static void pruneDocument( Document document, ItemFilter filter ) {
NodeList items = document.getElementsByTagName( "item" );
for ( int i = 0; i < items.getLength(); i++ ) {
Node node = items.item(i);
Node parent = node.getParentNode();
if( !filter.accept( (Element)node ) )
parent.removeChild( node );
}
}
Thanks for any advice, Neil