[jdom-interest] Moving Subtrees using JDOM

Rocker Bill r0cker at hotmail.com
Sat Feb 23 00:36:25 PST 2002


Without getting myself too lost, I am trying to move a sequence of elements 
with associated content from one top level parent to another, within the 
same document... such as...


In the above case, I want to move the subtree containing ele2 and ele3 from 
the parent, ele1, to beneath the new parent ele4 (ele2 and ele3 will also 
have associated content).

It sounds like what you guys are suggesting is what I am after.  From my 
limited understanding of the clone() method... it just clones one element 
and not the entire subtree below an element too(??)... so I guess an 
iterative approach would be most logical -- using Lists(??).

Am I correct to assume, you are suggesting to recursively collect the 
children from the old parent within a List, then recursively add each 
element under the new parent (detaching every element from the original 
parent along the way)??  Then just use oldParentEle.removeChildren() to 
remove the previous subtree structure from the original parent element? To 
sound naive, what would you suggest as the best way to maintain the original 
hierarchy of the subtree under the new parent element?? -- Or am I 
completely off the path you are both discussing?


>From: Jason Hunter <jhunter at acm.org>
>To: Elliotte Rusty Harold <elharo at metalab.unc.edu>
>CC: Rocker Bill <r0cker at hotmail.com>, jdom-interest at jdom.org
>Subject: Re: [jdom-interest] Moving Subtrees using JDOM
>Date: Fri, 22 Feb 2002 11:26:34 -0800
>Elliotte Rusty Harold wrote:
> >
> > At 10:12 AM -0800 2/22/02, Jason Hunter wrote:
> > >An element retains its children when it's moved, so I don't know what
> > >problem you could be having.  Just detach the element from its own
> > >parent and splice it somewhere else, and all its children will tag 
> > >for the ride.
> > >
> >
> > I think what he's asking for is something more akin to
> > DocumentFragfment in DOM; i.e. he doesn't want to move an Element. He
> > wants to move a sequence of three elements, two text nodes, a
> > comment, and perhaps a few more text nodes. You might be able to do
> > this using Lists. I'm not sure.
> >
> > OK, I just looked at the JavaDoc and I don't think we can do that, at
> > least not in the most general case. I think what we'd need would be
> > an addContent(List) method. We have setContent(List) but not
> > addContent(list). You could do it in two steps by retrieving the
> > content of the element you wanted to put it in with getContent() and
> > then using the methods of the List interface.
>Kind of like:
>Except the content being moved will have parents so this won't work
>exactly.  You'll have to iterate over them and detach each before the
>move.  Might as well just set up an iterator and do a detach/add for
>each right there.
>To control your jdom-interest membership:

Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.

More information about the jdom-interest mailing list