[jdom-interest] Removing 'Element's from a list during iteration

Alex Rosen arosen at silverstream.com
Thu Dec 13 12:14:46 PST 2001


Your code looks fine. The list returned by getChildren() was previously a
class called PartialList, which wasn't a particularly good implementation.
It looks like Jason just merged in the new FilterList implementation, which
is much better. Try building JDOM from the latest source code, that should
help a lot.

Alex

> -----Original Message-----
> From: jdom-interest-admin at jdom.org
> [mailto:jdom-interest-admin at jdom.org]On Behalf Of Alastair Rodgers
> Sent: Tuesday, December 11, 2001 10:02 AM
> To: 'jdom-interest at jdom.org'
> Subject: [jdom-interest] Removing 'Element's from a list during
> iteration
>
>
> I'm using an Iterator to navigate a list of child elements.
> As I do this, I want to remove certain of these children from
> the JDOM tree based on whether or not some attribute is
> present. Since Element.getChildren(String) returns a "live"
> list, I had expected to be able to do this with
> Iterator.remove() as follows:
>
>     Element elBase = ...
>     ...
>     for (Iterator iter =
> elBase.getChildren("SomeElement").iterator(); iter.hasNext(); ) {
>       Element el = (Element)iter.next();
>       if (el.getAttributeValue("SomeAttribute") == null) {
>         iter.remove();
>       }
>     }
>
> However, I find that after calling iter.remove() if I look at
> the parent of 'el', it is still elBase, i.e. it hasn't been
> properly detached. If I replace the call to "iter.remove()"
> with "el.detach()" (which has the effect of invoking
> elBase.removeContent(el)) then it appears to work - el's
> parent is now null. However, this violates the general usage
> pattern of Iterator, as the following extract from the
> Javadoc for Iterator.remove() illustrates:
>
> <quote>
> The behavior of an iterator is unspecified if the underlying
> collection is modified while the iteration is in progress in
> any way other than by calling this method.
> </quote>
>
>
> Is this the expected behaviour? If so, is it safe for me to
> substitute el.detach() in the above example, and ignore the
> J2SE Javadoc?
>
> Thanks in advance,
> Al.
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/yo
uraddr at yourhost.com




More information about the jdom-interest mailing list