[jdom-interest] How do I re-order child elements?
Ken Rune Helland
kenh at csc.no
Tue May 15 08:36:26 PDT 2001
At 03:32 PM 5/15/2001 +0100, Thomas Nichols wrote:
>I need to re-order children of an Element. Since el.addContent() always
>seems to _append_, what's the best way to do this?
>Method 1: use getMixedContent() to get a List, then el.removeChildren(),
>then el.addContent() for the Elements from the List.
>Element newEl = el.clone();
>newEl.addContent ( /* from el.getChildren().iterator() */ );
>Then copy everything back from newEl to el.
>Neither looks very attractive, nor efficient - I'd guess method 2 would be
>a dog. Anyone have any better suggestions?
>Would there be a more general use for an Element.addContent (int index,
>Element content) ? This would allow plenty of scope for re-ordering and
>sorting at minimal (apparent) cost, and would only need passing down to
>LinkedList.add (int index, Object o). To reposition an element, just
>el.addContent (index, child);
>What do you think? Do other people have this requirement?
Not sure if this currently works but you _shoud_
be able to do
List children = el.getChildren()
and the use normal List metods to rearrange the
More information about the jdom-interest