Re: Possible bug in JDom2

Rolf Lear <[email protected]> Thu, 15 Mar 2012 17:59:45 -0400
Newsgroups gmane.comp.java.jdom.general
Message-ID <[email protected]>
Hi Craig.

getDescendants returns an Iterator<Element> not an Iterable<Element>

Now that I think about it, it is a mess, but, that's because JDOM 1.x 
returned an iterator.

Technically your code should be:

for (Iterator<Element> it =root.getDescendants(tableFilter); 
it.hasNext(); ) {
     tableCount++;
}

I wonder whether I can make an 'Iterable' return value too.... it makes 
sense to, but I can't change the current return value for getDescendants 
without breaking compatibility...


suggestions?

Rolf



On 15/03/2012 3:59 PM, Craig Noah wrote:
> I've downloaded the latest JDom2 beta today and am working to 
> incorporate it into some new code.  I am developing against Java6, so 
> I would expect iterators to work.  However, the following code fails 
> to compile (with JDom2 includes):
>
> SAXBuilder sax = new SAXBuilder();
> Document xml = sax.build (source);    // source is a File object
> Element root = xml.getRootElement();
> ElementFilter tableFilter = new ElementFilter ("Table");
> int tableCount = 0;
> for (Element table : root.getDescendants(
> tableFilter)) {
>     tableCount++;
> }
>
> The compile-time error that I get states, "Can only iterate over an 
> array or an instance of java.lang.Iterable".  Since 
> Element.getDescendants (Filter<F>) returns a java.util.Iterator<F>, I 
> would expect my code to compile and work.  What am I missing?
>
> Sincerely,
> Craig
>
>
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/[email protected]

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/[email protected]