[jdom-interest] How do I re-order child elements?

Thomas Nichols thomas.nichols at iname.com
Tue May 15 07:32:36 PDT 2001

Good Day,

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.

Method 2:
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.removeContent (child);
el.addContent (index, child);

What do you think? Do other people have this requirement?


More information about the jdom-interest mailing list