[jdom-interest] new element methods?

Bryan Thale bryan.thale at motorola.com
Tue Jul 15 09:27:14 PDT 2003


The problem is that what to do when the parsing fails or the element is 
missing is largely application dependent.  In your example 
implementations, you assume default values such as false, 0, and "" 
which may work great for your apps, but I may want to return NaN or an 
exception instead.  It is a simple matter to create a class with static 
methods to implement your type safe methods as in:

public abstract class MyApp
{
  public static final float getFloat( Element element, String name){
    try{
      return Float.parseFloat(element.getChildTextTrim(name));
    }
    catch(Exception ex){
      return 0f;
    }
  }
}

...

float myFloat = MyApp.getFloat( e, name );

If all of your apps require the same behavior, this is a write once and 
use forever class.  However, if you need different logic for different 
apps, you are free to define as many of these parsing classes as 
required without getting JDOM involved.  I suppose you could extend 
Element instead, but that seems like more work than it is worth for such 
a simple task.

Bryan.

-- 
Bryan Thale
Networks & Infrastructure Research, Motorola Labs
mailto:bryan.thale at motorola.com





More information about the jdom-interest mailing list