[jdom-interest] retrieving an element based on an attribute

bob mcwhirter bob at werken.com
Mon Mar 4 20:50:07 PST 2002


When in doubt, XPath (http://jaxen.org/)

XPath xpath = new XPath( "/thingies/fibonacci[@index='12']" );

xpath.selectSingleNode( myDoc );

	-bob


On Mon, 4 Mar 2002, Collin Peters wrote:

> Newbie question:
> 
> Is there any way to retrieve an element based on an attribute beside looping
> though the possible elements and looking for the attribute?
> 
> For example, in the following text from the javaworld.com documentation:
> 
> <fibonacci index="8">21</fibonacci>
> <fibonacci index="9">34</fibonacci>
> <fibonacci index="10">55</fibonacci>
> <fibonacci index="11">89</fibonacci>
> <fibonacci index="12">144</fibonacci>
> <fibonacci index="13">233</fibonacci>
> <fibonacci index="14">377</fibonacci>
> <fibonacci index="15">610</fibonacci>
> <fibonacci index="16">987</fibonacci>
> <fibonacci index="17">1597</fibonacci>
> 
> Is there a one line piece of code to get the fibonacci number at index 12?
> The only other way I see to do it is to get the children where the element
> name is fibonacci and then loop through looking for the attribute where
> index=12
> 
> Collin Peters
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com
> 




More information about the jdom-interest mailing list