Alignment: Chaotic Java

Sunday, December 11, 2005

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:

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?


At 12/12/2005 01:34:00 AM, Blogger nameless_1 said...

i hear you, i like JAXP, but i think it lacks a lot of convenience methods. i wrote two or three mehods for making it as easy as Dom4j for simple Xml parsing.
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.


Post a Comment

Links to this post:

Create a Link

<< Home