[jdom-interest] Possible bug in JDom2

Rolf Lear jdom at tuis.net
Thu Mar 15 16:03:31 PDT 2012

Hi Craig.

I think my previous reply was a bit hurried... thinking about it more, I 
get the following...

- It would be nice to return 'Iterable', because that would make the 
'enhanced' for possible.... but how important is the enhanced for?
- it would require a new method (actually two) in org.jdom2.Parent
- it would also be nice to have a method getSelfAndDescendants()
- but that would be complicated on Document, because 'self' is not 
Content (it's Document).

So, I think the idea of getSelfAndDescendants is nice, but not viable.

I think the idea of a new method returning an Iterable is nice, and it 
is viable, but is it necessary? I am not convinced.... yet.

The "enhanced for" (for-each) loop is just a convenience. It technically 
does not add any new functionality (other than a 'simpler' line of 
code). Is it really that much harder to do:

for (Iterator<Element> it = element.getDescendants(filter);
          it.hasNext(); ) {
    Element e = it.next();
    // do something.


for (Element e : element.getDescendantIterable(filter)) {
    // do something

Actually, looking at the above, it is a significant difference, I guess.

Ok, I am more convinced than before... maybe it would be useful, but 
then it will be messy too to have the 'old' getDescendants() methods too.

I will think about it some more... if there was a 'logical' way to 
express these new methods (good, meaningful, non-confusing names) it 
would make it an easier decision...

What would be a good name?

While I have your code example 'in mind' I thought I would point out a 
couple of other things....

Have you seen there is the org.jdom2.Filters class? It makes some other 
lines simpler too. Instead of:

ElementFilter tableTilter = new ElementFilter("Table");

you can do:

Filter<Element> tableFilter = Filters.element("Table");

Well, that's not exactly better... the exact same number of 
characters.... oh, it does make a difference if you do not 'keep' the 
tableFilter instance...

Iterator<Element> it = root.getDescendants(Filters.element("Table"));

Another thing, in your example you could possibly consider an XPath...

XPathExpression<?> xp = XPathFactory.compile("//Table")
int tablesize = xp.evaluate(root).size();

If you want the results as a list of Element:

XPathExpression<Element> xp = XPathFactory.compile(
        ".//Table", Filters.element())
int tablesize = xp.evaluate(root).size();

Anyway, there is some food for thought in all of this.


On 15/03/2012 5:59 PM, Rolf Lear wrote:
> 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/youraddr@yourhost.com
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com

More information about the jdom-interest mailing list