[jdom-interest] Problem with Jdom8 - getChildren() on root do esn't work...

Bradley S. Huffman hip at a.cs.okstate.edu
Mon Apr 8 12:29:23 PDT 2002


"Beleznay, Dave" writes:

> Did this ever get resolved?

The part about EJB and a possible serialization problem? Not yet.

> I seem to be having the same problem when
> I'm trying getChildren() on the root element.
> the List children contains no elements after the assignment. 

He's you code annotated with with what the current design
should produce and why.

// get a 'live' list of rootElement's content, by 'live' we mean changes
// it, also show in rootElement and any other 'live' list obtained from
// a call to getChildren or getContent on the same element. getChildren
// returns a view of the Elements content where only objects of type
// Element are visiable. Since it is live however, children.add(someElement)
// produces the same result as rootElement(someElement).
List children = rootElement.getChildren();

// removes all Element children from rootElement, which means all items are
// also removed from List children, rootElement may still have content it
// just won't be a instanceof Element.
rootElement.removeChildren();

// this produces a iterator where hasNext() initially will return false
// since children is empty
ListIterator i = children.listIterator();

// hasNext() is false so the loop is never executed
while (i.hasNext()) {
    Element ele = (Element) i.next();
    String eleName = ele.getName();
    if (eleName.equals("role")) {
        i.next();   // I assume this is a typo, otherwise you have 2 next()
                    // in a row without a corresponding hasNext() 
        i.add(roleDesigner);
    }
}

// children is empty so this does nothing
rootElement.setChildren(children);

Little long winded but there seems to be some confussion caused by having
all Lists 'live', guess we need to improve the docs.

Brad



More information about the jdom-interest mailing list