[jdom-interest] Moving elements closer to the root

Thomas Nichols thomas.nichols at iname.com
Mon May 7 17:37:30 PDT 2001


Good Day,
Previous discussions on detach() et al haven't yet guided me to a solution 
to this one - all help welcome.
I want to convert
<a>
   <b>
     <c>
     Some mixed content.
     </c>
   </b>
</a>

to
<a>
   <c>
   Some mixed content.
   </c>
</a>

i.e. I want to make the _child_ part of the content of the _grandparent_, 
making the parent redundant.

If Element grandParent is <a>, parent is <b> and child is <c>, I'm trying 
the following:

grandParent.removeContent (parent);
grandParent.addContent (child);

The exception thrown is:

org.jdom.IllegalAddException: The element "c" could not be added as a child 
of "a": The element already has an existing parent "b".

So... how do I tell child that its parent has changed? Element.setParent is 
protected, do I have to subclass and do a
child.setParent (grandParent) ? The rest of JDOM seems pretty clean, I'm 
hoping there's a straightforward way to do this? Perhaps using getCopy() or 
clone()?

Excellent API, thanks a lot.
Regards,
Thomas.




More information about the jdom-interest mailing list