public Element next(){
  if (elementStack == null) {
    return first();
  }
  if (elementStack.isEmpty()) {
    return null;
  }
  StackItem item=elementStack.peek();
  Element elem=item.getElement();
  int index=item.getIndex();
  if (index + 1 < elem.getElementCount()) {
    Element child=elem.getElement(index + 1);
    if (child.isLeaf()) {
      item.incrementIndex();
    }
 else {
      elementStack.push(new StackItem(child));
    }
    return child;
  }
 else {
    elementStack.pop();
    if (!elementStack.isEmpty()) {
      StackItem top=elementStack.peek();
      top.incrementIndex();
      return next();
    }
  }
  return null;
}

public Element next ( ) { if ( elementStack == null ) { return first ( ) ; } if ( elementStack . isEmpty ( ) ) { return null ; } StackItem item = elementStack . peek ( ) ; Element elem = item . getElement ( ) ; int index = item . getIndex ( ) ; if ( index + 1 < elem . getElementCount ( ) ) { Element child = elem . getElement ( index + 1 ) ; if ( child . isLeaf ( ) ) { item . incrementIndex ( ) ; } else { elementStack . push ( new StackItem ( child ) ) ; } return child ; } else { elementStack . pop ( ) ; if ( ! elementStack . isEmpty ( ) ) { StackItem top = elementStack . peek ( ) ; top . incrementIndex ( ) ; return next ( ) ; } } return null ; }
fetches the next element . the strategy used to locate the next element is a depth - first search .
