[From nobody Mon Sep 20 17:38:33 2004 X-Mozilla-Status2: 00000000 Message-ID: <414F7727.9D415FAC@mitre.org> Date: Mon, 20 Sep 2004 20:34:47 -0400 From: Mike Brenner <mikeb@mitre.org> Organization: none X-Mailer: Mozilla 4.79 [en]C-20020130M (Windows NT 5.0; U) X-Accept-Language: en MIME-Version: 1.0 To: Per Norrman <per.norrman@austers.se> Subject: Re: Fwd: Re: [jdom-interest] XML Signature References: <414B11D3.4080104@xquery.com> <414B6FE2.408@austers.se> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit This does not work like, say, getElementById in an xml svg file, which searches all levels. This code only searches one level. > > 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. > > Per Norrman wrote: > 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. ]