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

Alex Rosen arosen at silverstream.com
Wed Jul 25 13:47:40 PDT 2001


The list returned by getChildren() is a live list. Your new node becomes
a child of the parent node, as soon as you add it to the list "l". When
you then say setChildren(), you're asking it to add a child to Lnks
that's already a child of it, so it's giving you an error. (It might be
a little clearer if we were to check for this case, and give a better
message, like: "The element "Lnk" is already a child of "Lnks". It
cannot be added to the same parent twice.")

> -----Original Message-----
> From: jdom-interest-admin at jdom.org
> [mailto:jdom-interest-admin at jdom.org]On Behalf Of Jake Latham
> Sent: Wednesday, July 25, 2001 4:14 PM
> To: jdom-interest at jdom.org
> Subject: [jdom-interest] Help - Trouble with adding children via
> setChildren...
>
>
> 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.
>    l.add(el);
>
>    for(int i=0;i<l.size();i++)
>    {
>     Element theE = (Element)l.get(i);
>     System.out.println("Name:" + theE.getName() + " Text: " +
> theE.getText());
>    }
>
>
>    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
> <Lnks>
>   <Lnk>TX_TX_Low-TX_PCA</Lnk>
> </Lnks>
>
> ...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...
>
> -Jake
> RockySoft Corporation
> 2301 Research Boulevard - Suite 202
> Jake Latham
> Code Monkey
> Phone:  (970) 493-0868 x112
> Fax:  (970) 493-0887
>
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/yo
uraddr at yourhost.com




More information about the jdom-interest mailing list