[jdom-interest] new element methods?

William Krick wkrick at eio-online.com
Tue Jul 15 10:57:01 PDT 2003


Phill addressed this problem in his implementation a few emails back.

Essentially, his getXXX methods also take a default value that it returns in
the case where it fails...


getInt(String name, int default) {
  int result;
  try{
    result = Integer.parseInt(getChildTextTrim(name));
  }
  catch(Exception ex){
    result = default;
  }
  return result;
}


...
Krick






> -----Original Message-----
> From: jdom-interest-admin at jdom.org
> [mailto:jdom-interest-admin at jdom.org]On Behalf Of Bryan Thale
> Sent: Tuesday, July 15, 2003 1:50 PM
> To: jdom-interest at jdom.org
> Subject: Re: [jdom-interest] new element methods?
>
>
> 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
>
>
>
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/yourad
dr at yourhost.com




More information about the jdom-interest mailing list