[jdom-interest] new to jdom .. and many questions

Bradley S. Huffman hip at a.cs.okstate.edu
Tue Aug 27 20:24:55 PDT 2002


Azrael writes:

> finding next sibling as you say is easy enough.. but I am a little 
> unsure about how to navigate the jdom tree breadth first using the 
> Lists.. does anyone have example code that I could use as a guide?

Hmmm, off the top of my head, how about

public class BreadthFirst {

    private List mylist = new ArrayList();
    private int cursor = 0;

    public BreadthFirst(Element start) {
        mylist.add(start);
    }

    public Object next() {
        int size = mylist.size();

        // First check if we have run out of items. If so reload list
        // with children of all the Elements.
        if ((size != 0) && (cursor >= size)) {
            List temp = new ArrayList();
            for (int i = 0; i < size; i++) {
                if (mylist.get(i) instanceof Element) {
                    Element element = (Element) mylist.get(i);
                    temp.addAll(element.getContent());
                }
            }
            mylist = temp;
            cursor = 0;
        }

        // Anything in the list? No, return null
        if (size == 0) {
            return null;
        }

        // Yes, return item pointed to by cursor.
        if (cursor < size) {
            cursor++;
            return mylist.get(cursor - 1);
        }
    }
}



More information about the jdom-interest mailing list