[jdom-interest] How do I re-order child elements?
Steven D. Keens
skeens at planetfred.com
Tue May 15 08:41:09 PDT 2001
If my understanding of JDOM is correct, you could use Collections.sort().
Comparator comp = new YourComparator();
List children = el.getMixedContent();
Collections.sort( children, comp );
The children should then be sorted according to the order you
specifiy in YourCmparator.
Steven Keens mailto:skeens at planetfred.com
Planetfred Inc. http://www.planetfred.com
44 Byward Market, Suite 240, Ottawa, ON, K1N 7A2, Canada
>From: jdom-interest-admin at jdom.org
>[mailto:jdom-interest-admin at jdom.org]On Behalf Of Thomas Nichols
>Sent: Tuesday, May 15, 2001 10:33
>To: JDOM Interest
>Subject: [jdom-interest] How do I re-order child elements?
>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?
>To control your jdom-interest membership:
r at yourhost.com
More information about the jdom-interest