[jdom-interest] new element methods?

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


I was wondering if it might be useful to add some methods to Element...

  getInt(java.lang.String name)

  getFloat(java.lang.String name)

  getBoolean(java.lang.String name)

  getString(java.lang.String name)
  
  etc...

...that are essentially aliases for...

  getChildTextTrim(java.lang.String name) 

...but with added code that tries to parse the child text string
to the appropriate type...


  public boolean getBoolean(String name){
    boolean result;
    try{
      result = getChildTextTrim(name).equalsIgnoreCase("true");
    }
    catch(Exception ex){
      result = false;
    }
    return result;
  }

  public String getString(String name){
    String result = getChildTextTrim(name);
    if(result == null)
      return "";
    return result;
  }

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

  public float getFloat(String name){
    float result;
    try{
      result = Float.parseFloat(getChildTextTrim(name));
    }
    catch(Exception ex){
      result = 0f;
    }
    return result;
  }

  etc...


...
Krick



More information about the jdom-interest mailing list