[jdom-interest] Help - Trouble with adding children via setChildren...

Jake Latham jlatham at rockysoft.com
Wed Jul 25 13:13:56 PDT 2001

Hiya all.

I have a JDOM element called 'connectors', that I'm trying to add children
to.  It already contains "<Lnks></Lnks>" when it is constructed, and I'm
trying to add "<Lnk>" Elements to it as children.  It seems to work OK the
first go-round, but as soon as I try to add a second child, it always flips
out on me, giving an Illegal Add Exception.  I thought it might be a
duplication thing,(should that matter since it's XML?) but I can't figure
out how.  Here's some code:

   List l = p.connectors.getChildren("Lnk");

   if(l.size() ==0)
    l = new com.sun.java.util.collections.ArrayList();

   Element el = new Element("Lnk");
   el.setText("TX_"+connName);  //this is guaranteed to be unique from any
former element's text.

   for(int i=0;i<l.size();i++)
    Element theE = (Element)l.get(i);
    System.out.println("Name:" + theE.getName() + " Text: " +

   p.connectors.setChildren(l); //dies here when adding #2.
   XMLOutputter oP = new XMLOutputter("  ", true);

here's some sample output:

Added Node:'TX_TX_Low-TX_PCA' to the scene Root
Name:Lnk Text: TX_TX_Low-TX_PCA

...does some more stuff, including skipping dupliciate names...

Added Node:'TX_TX_Low-TX_Component' to the scene Root
Name:Lnk Text: TX_TX_Low-TX_PCA
Name:Lnk Text: TX_TX_Low-TX_Component
Exception occurred during event dispatching:
org.jdom.IllegalAddException: The element "Lnk" could not be added as a
child of "Lnks": The element already has an existing parent "Lnks"

What is it I'm doing wrong?

Any help is appreciated...

RockySoft Corporation
2301 Research Boulevard - Suite 202
Jake Latham
Code Monkey
Phone:  (970) 493-0868 x112
Fax:  (970) 493-0887

More information about the jdom-interest mailing list