IMHO It it still better to use the iterator. This removed the need for the additional list and array creation. As I said before you need to romove the object using the iterator, so in your example
Iterator elementIterator = root.elementIterator(); while(elementIterator.hasNext()){ Element element = (Element)elementIterator.next(); String name = element.attributeValue("name"); System.out.println(name); if(!dict.containsKey(name)){ System.out.println(name + " is an extra element..."); root.remove(element); //element.detach(); System.out.println("Deleted"); } } Should be while(elementIterator.hasNext()){ Element element = (Element)elementIterator.next(); String name = element.attributeValue("name"); System.out.println(name); if(!dict.containsKey(name)){ System.out.println(name + " is an extra element..."); elementIterator.remove(); ///******* removed using the iterator //root.remove(element); //element.detach(); System.out.println("Deleted"); } } BTW are you merely trying to remove all elements with duplicated names? Is so then you have find them all using an Xpath statement -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of David D. Lucas Sent: Wednesday 18 February 2004 13:19 To: Ray Grieselhuber Cc: [EMAIL PROTECTED] Subject: Re: [dom4j-user] java.util.ConcurrentModificationException Instead of using an iterator, try an object array list and iterate over the object array. Element root = doc.getRootElement(); List list = root.elements(); if (list!=null) { Element item=null; Object[] listArray = list.toArray(); for (int i = 0; i < listArray.length; i++ ) { item=(Element)listArray[i]; //do something or remove it } } Ray Grieselhuber wrote: > I modified the code to do this: > > Element root = doc.getRootElement(); > Iterator elementIterator = root.elementIterator(); > while(elementIterator.hasNext()){ > Element element = (Element)elementIterator.next(); > String name = element.attributeValue("name"); > System.out.println(name); > if(!dict.containsKey(name)){ > System.out.println(name + " is an extra element..."); > root.remove(element); > //element.detach(); > > System.out.println("Deleted"); > } > } > > But I still get the same error. Any ideas? > Thanks, > Ray > > > Mike Skells wrote: > >> Hi Ray, Use remove() on the iterator >> The message is correct. You are modifying the collection while the >> iterator is operating. CME is thrown if any modification is make to the >> collection other than through the iterator >> >> Mike Skells >> >> -----Original Message----- >> From: [EMAIL PROTECTED] >> [mailto:[EMAIL PROTECTED] On Behalf Of Ray >> Grieselhuber >> Sent: Tuesday 17 February 2004 17:58 >> To: [EMAIL PROTECTED] >> Subject: [dom4j-user] java.util.ConcurrentModificationException >> >> >> Hello, >> I am trying to remove nodes on a large set of data: >> >> while(elementIterator.hasNext()){ >> Element element = (Element)elementIterator.next(); >> String name = element.attributeValue("name"); >> System.out.println(name); >> if(!dict.containsKey(name)){ >> System.out.println(name + " is an extra >> element..."); >> >> element.detach(); >> } >> } >> >> I am getting this error: >> >> java.util.ConcurrentModificationException >> at >> java.util.AbstractList$Itr.checkForComodification(AbstractList.java:444) >> at java.util.AbstractList$Itr.next(AbstractList.java:417) >> at org.dom4j.tree.FilterIterator.findNext(FilterIterator.java:70) >> at org.dom4j.tree.FilterIterator.next(FilterIterator.java:45) >> at perfanalysis.SkinnyFiles.main(SkinnyFiles.java:55) >> Exception in thread "main" >> >> I am using dom4j 1.4. Thanks! >> Ray >> >> >> ------------------------------------------------------- >> SF.Net is sponsored by: Speed Start Your Linux Apps Now. >> Build and deploy apps & Web services for Linux with >> a free DVD software kit from IBM. Click Now! >> http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click >> _______________________________________________ >> dom4j-user mailing list >> [EMAIL PROTECTED] >> https://lists.sourceforge.net/lists/listinfo/dom4j-user >> >> >> >> >> ------------------------------------------------------- >> SF.Net is sponsored by: Speed Start Your Linux Apps Now. >> Build and deploy apps & Web services for Linux with >> a free DVD software kit from IBM. Click Now! >> http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click >> _______________________________________________ >> dom4j-user mailing list >> [EMAIL PROTECTED] >> https://lists.sourceforge.net/lists/listinfo/dom4j-user >> >> >> >> >> > > > > ------------------------------------------------------- > SF.Net is sponsored by: Speed Start Your Linux Apps Now. > Build and deploy apps & Web services for Linux with > a free DVD software kit from IBM. Click Now! > http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click > _______________________________________________ > dom4j-user mailing list > [EMAIL PROTECTED] > https://lists.sourceforge.net/lists/listinfo/dom4j-user > -- +------------------------------------------------------------+ | David Lucas mailto: ddlucas @ lse.com | | Lucas Software Engineering, Inc. (740) 964-6248 Voice | | Unix,Java,C++,CORBA,XML,EJB (614) 668-4020 Mobile | | Middleware,Frameworks (888) 866-4728 Fax/Msg | +------------------------------------------------------------+ | GPS Location: 40.0150 deg Lat, -82.6378 deg Long | | IMHC: "Jesus Christ is the way, the truth, and the life." | | IMHC: "I know where I am; I know where I'm going." <>< | +------------------------------------------------------------+ Notes: PGP Key Block=http://www.lse.com/~ddlucas/pgpblock.txt IMHO="in my humble opinion" IMHC="in my humble conviction" All trademarks above are those of their respective owners. ------------------------------------------------------- SF.Net is sponsored by: Speed Start Your Linux Apps Now. Build and deploy apps & Web services for Linux with a free DVD software kit from IBM. Click Now! http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click _______________________________________________ dom4j-user mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/dom4j-user ------------------------------------------------------- SF.Net is sponsored by: Speed Start Your Linux Apps Now. Build and deploy apps & Web services for Linux with a free DVD software kit from IBM. Click Now! http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click _______________________________________________ dom4j-user mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/dom4j-user