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

Azrael azrael at azrael-uk.f2s.com
Tue Aug 27 21:07:36 PDT 2002


Bradley S. Huffman wrote:
> 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);
>         }
>     }
> }

Thanks! and Wow! for the fast response!
it's 5am for me now.. so there's no way I'm going to dare attempt 
working some more on my project except conceptually (I spent a good few 
weeks conceptualising about my project while the Football World Cup was 
on earlier in the summer :) but I'll take a look at it in the morning 
and let you know if I want to have your kids :)

but it looks as if I could add a hasMoreElements() to that class and 
kill two birds with one stone (or kill one project with one class ... :)

-- 
                 Azrael

            ("\''/").___..--'''"-._
            `0_ O  )   `-.  (     ).`-.__.`)
            (_Y_.)'  ._   )  `._ `. ``-..-'
          _..`--'_..-_/  /--'_.' .'
         ((i).-''  ((i).'  (((.-'

Of all God's creatures there is only one that cannot be made the slave 
of the lash. That one is the cat. If man could be crossed with a cat it 
would improve man, but it would deteriorate the cat.

ICQ#52944566




More information about the jdom-interest mailing list