org.w3c.dom should be deprecated
.... Or at least changed to be more easy to use. Take NodeList as an example. Why isn't it Iterable on J2SE 5? And even in J2SE 1.4, why doesn't NamedNodeMap implement NodeList, even though they share the same methods for "iteration" (getLength and item)?
I wrote this simple class. Hope you find it useful:
Sorry about the bad format of the code. Anyone knows of a good code formatter to use for my code copy/pastes?
I wrote this simple class. Hope you find it useful:
class NodeListIterator implements Iterator<Node> {
private NodeList list;
private int idx;
private int len;
public NodeListIterator(NodeList list) {
this.list = list;
this.idx = 0;
this.len = list.getLength();
}
public boolean hasNext() { return idx < len; }
public Node next() { return list.item(idx++); }
public void remove() { throw new UnsupportedOperationException(\"Can't remove\"); }
public static Iterable<Node> iterate(final NodeList list) {
return new Iterable<Node>() {
public Iterator<Node> iterator() { return new NodeListIterator(list); }
}
}
}
Sorry about the bad format of the code. Anyone knows of a good code formatter to use for my code copy/pastes?
Comments (1)
as you said, NodeList is neither a "List" nor iterable so, i made a stupid converter method going through the items and adding them to the a list. Your method seems like a better idea.