[jdom-interest] Maybe a bug report related to the "detach" method...

Carlos Barroso est-c-barroso at ptinovacao.pt
Thu Apr 11 02:22:19 PDT 2002


He's a simple example that gets a root element and traverses it's
children. And when it finds an element with an attribute "name" with
the value "reporter", it detaches the element.
But I think that the "detach()" method is not working as expected.

NOTE: I used the jdom beta 8 

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

import java.util.List;
import java.util.Iterator;

import java.io.*;

public class JDOMDemo {
    public static void main(String[] args) {
        try {
            SAXBuilder builder = new SAXBuilder();
            Document doc = builder.build( "signon.xml" );
            Element root = doc.getRootElement();
	      List kids = root.getChildren();
	      Iterator it = kids.iterator();
	      while(it.hasNext()) {
	        Element elem = (Element)it.next();
	        if(elem.getAttributeValue("name").equals("reporter")) {
		    elem.detach();
              }	   
            }

	    XMLOutputter out = new XMLOutputter();
	    out.setIndent("  ");
	    out.setNewlines(true);
	    out.output(doc, System.out);

        } catch (JDOMException e) {
            e.printStackTrace();
        } catch(IOException e) {
	      System.out.println(e.getMessage());
        }
    }
}

Executing this piece of code I get the following error:

Exception in thread "main" java.util.ConcurrentModificationException
	at
org.jdom.ConcurrentList$FilterListIterator.checkConcurrentModification( ...
)
	at org.jdom.ContentList$FilterListIterator.hasNext( ... )
	at JDOMDemo.main(JDOMDemo.java:25)

I read the FAQ and I saw that we can use the methods contained in the "List"
class, so, 
I tried to substitute the line "elem.detach();" with "kids.remove(elem);".
And it gave me the exact same error.



More information about the jdom-interest mailing list