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

Bradley S. Huffman hip at a.cs.okstate.edu
Mon Apr 8 15:54:25 PDT 2002


"Beleznay, Dave" writes:

> but I've still got a couple of questions for how it *should* be working,
> as I'm not sure i'm reading you correctly...

That's okay I no right so good :-;
  
> if I were to do a children.add(someElement) that's the same as
> rootElement(someElement)?  do you mean
> rootElement.addContent(someElement) ?

Yes, I don't type so good either.

> If I were to write the code the same way :
> 
>                 List children = rootElement.getChildren();
> // rootElement.removeChildren();
>                 ListIterator i = children.listIterator();
>                 
>                 while (i.hasNext()) {
>                     Element ele = (Element) i.next();
>                     String eleName = ele.getName();
>                     if (eleName.equals("role")) {
>                         i.next();    
>                         i.add(roleDesigner);
>                     }
>                 }
>                 rootElement.setChildren(children);                
> 
> only removing the line to removeChildren(),  then the last call to
> setChildren is basically doing nothing?

No, now it will through a IllegalAddException because the items in children
are already attached to a parent Element, rootElement in this case.

> Or should I be creating a new ArrayList and playing with that, as was
> suggested?

What are you wanting to do, add to the children of rootElement or completely
replace the children (i.e. objects of Element)?

     Case 1:
                 List children = rootElement.getChildren();
                 ListIterator i = children.listIterator();
                 
                 while (i.hasNext()) {
                     Element ele = (Element) i.next();
                     String eleName = ele.getName();
                     if (eleName.equals("role")) {
                         i.add(roleDesigner);
                     }
                 }
 
     Case 2:
                 List newChildren = new ArrayList();
                 List children = rootElement.getChildren();
                 ListIterator i = children.listIterator();
                 
                 while (i.hasNext()) {
                     Element ele = (Element) i.next();
                     String eleName = ele.getName();
                     if (eleName.equals("role")) {
                         newChildren.add(roleDesigner);
                     }
                 }
                 rootElement.setChildren(newChildren);
 
Now in JDOM parlance child, children refers to Elements only (not Text,Comment,
etc).  So if you do:

    Element e = new Element("test");
    e.addContent(new Comment("a comment"));
    e.addContent(new Element("a"));
    e.addContent(new Element("b"));
    e.addContent(new Element("c"));

    List l1 = e.getContent();   // size() is 4
    List l2 = e.getChildren();   // size() is 3

    l2.add( new Element("d"));

    List l1 = e.getContent();   // size() is now 5
    List l2 = e.getChildren();   // size() is 4

    l1.add( new Comment("another comment"));

    List l1 = e.getContent();   // size() is now 6
    List l2 = e.getChildren();   // size() is still 4

Hope these helps.

Brad



More information about the jdom-interest mailing list