[jdom-interest] JDOM and Collections.sort fails

Keld Helbig Hansen khh at maerskdata.dk
Fri Jul 5 03:45:41 PDT 2002

I get an error when running this program with beta 8:

SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(filename);
List dvds = doc.getRootElement().getChildren();
Collections.sort(dvds, new SortTitle());

where SortTitle is this:

package hansen.playground;

import org.jdom.*;
import java.util.*;

class SortTitle implements Comparator {
  public int compare(Object o1, Object o2) {
    Element d1 = (Element)o1;
    Element d2 = (Element)o2;
    int i = (d1.getChildText("title")).compareToIgnoreCase(d2.getChildText(
    return (d1.getChildText("title")).compareToIgnoreCase(d2.getChildText(

The error is :

org.jdom.IllegalAddException: The element already has an existing parent
"collection"      at org.jdom.ContentList.add(ContentList.java:190)     at
org.jdom.ContentList.add(ContentList.java:146)  at
org.jdom.ContentList.set(ContentList.java:663)  at
org.jdom.ContentList$FilterListIterator.set(ContentList.java:1150)      at
. . .

When running with Beta 7 it works fine.

Could this be a bug??


More information about the jdom-interest mailing list