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

Dennis Sosnoski dms at sosnoski.com
Tue Aug 27 21:31:18 PDT 2002


Hey, I like that - cleaner and probably more efficient than the direct 
navigation approach I suggested a few minutes ago. If the goal is to 
return only elements (as suggested in the email) you probably want to 
use "temp.addAll(element.getChildren())" rather than 
"temp.addAll(element.getContent())", though. This lets you skip the 
"mylist.get(i) instanceof Element" check, too, since you know there'll 
only ever be elements in the list.

  - Dennis

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);
>        }
>    }
>}
>_______________________________________________
>To control your jdom-interest membership:
>http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com
>
>  
>





More information about the jdom-interest mailing list