[jdom-interest] Convenience method for deep Element structures

Patrick Dowler Patrick.Dowler at nrc.ca
Tue Jul 25 10:21:21 PDT 2000


On Wed, 12 Jul 2000, Aleksi Kallio wrote:
> How about providing a method such as:
> 
> doc.getNestedElement("settings/database/admin/password")
> 
> (instead of 
> doc.getRootElement().getChild("database").getChild("admin").getChild("password"))

This sort of thing doesn't add any value to the API per se. The method could
be implemented in any utility class just as easily, which lets the core API
remain small. Somehting like this would do it:

class MyUtilityClass
{
	public String getNestedElement(Element base, String pathHack)
	{
		Element cur = base;
		// probably should trim the path, and maybe check for
		// leading and trailing /
		StringTokenizer st = new StringTokenizer(pathHack, "/");
		while (st.hasMoreTokens() )
		{
			cur = cur.getChild( st.nextToken() );
			if ( cur == null )
				return null; // failed
		}
		return cur.getContent(); // or cur.getText() in the proposed API
	}
}

It could even be static. Since getNestedElement can use the existing API
and doesn't need to know anything ab out elements, it is best left outside.
Also, XPath may be the way to do this anyway, so it would likely get
depractated later on...

--

Patrick Dowler
Canadian Astronomy Data Centre




More information about the jdom-interest mailing list