[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:
>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.removeChildren();
>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?
>
>Regards,
>Thomas.

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
children.


KenR
  




More information about the jdom-interest mailing list