Fwd: Re: [jdom-interest] XML Signature

Per Norrman per.norrman at austers.se
Fri Sep 17 16:14:42 PDT 2004


> I would much rather write
> 
> doc.getRootElement().getChild("partslist").getElementById("12345")
> 
> than
> 
> list = getRootElement().getChild("partsist").getChildren();
> for (int i = 0; i < list.size(); i++) {
>        Element e = list.get(i);
>        if e.getAttributeValue("id").equals(id)  etc.
> 

No, no, no. Document.getElementById(String id) would return the Element
that has an attribute whith type ID_ATTRIBUTE and which value is equal to
the id parameter. The name of the attribute is irrelevant.

It should behave exactly as org.w3c.dom.Document#getElementById(String id)

It is very easy to implement:

Element getElementById(String value) {
    for(Iterator i = getDescendants(); i.hasNext(); ) {
       Object node = i.next();
       if (node instanceof Element) {
          Element e = (Element) node;
          for(Iterator j = e.getAttributes().iterator(); j.hasNext();) {
             Attribute a = (Attribute) j.next();
             if (a.getAttributeType() == Attribute.ID_TYPE &&
                 value.equals(a.getValue())
             {
                return e;
             }
          }
       }
    }
    return null;
}

Probably performs like dog for large documents, but then you could enhance it
with Laurent's mapping solution.

/pmn



More information about the jdom-interest mailing list