[jdom-interest] new element methods?

William Krick wkrick at eio-online.com
Tue Jul 15 08:51:43 PDT 2003


So you actually extended the Element class, huh?

I was thinking about doing that.

Currently, I have a bunch of static methods in a Util class in my project.

The syntax is a little longer that way...

boolean value = Util.getXMLBoolean(element, "TAGNAME");

I think I'd rather extend Element as you did.

Just a organizational question...

What do you call your extended Element and where is it in your package
hierarchy?

I've seen people create things like "EnhancedElement" or "MynameElement" and
put them in a com.myname.toolkit or com.myname.util package.


As for your internationalization problem, you might want to look at
java.text.DecimalFormat.parse()

Supposedly, you can get set it up to work with different locales.

You'd have to somehow know what locale the data was coming from before you
parsed it.

You might be able to represent a float in exponential notation, I think that
it's standardized on a '.' instead of a ','

...
Krick

  -----Original Message-----
  From: Phill_Perryman at Mitel.COM [mailto:Phill_Perryman at Mitel.COM]
  Sent: Tuesday, July 15, 2003 11:01 AM
  To: William Krick
  Subject: Re: [jdom-interest] new element methods?



  I would agree up to the point you have the float. Is there an accepted way
to represent a float which does not have an "." or "," in it.

  My biggest problem is that a file created in Europe would have 1.234,00
whereas a uk file would have 1,234.00. Parse will assume the local machine
format and so a file sent from one machine to another will fail. I am only
storing currency at the moment so I am storing it as

  return (getInt(attName, 0)/100) (it may be ugly but it works)

  My extended Element class already has them but I have a

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

  I would be in favour of adding these in s I personally use them quite a
lot.


  /Phill
  IS Dept, Software Engineer.
  phill_perryman at mitel.com
  http://www.mitel.com
  Tel: +44 1291 436023


       "William Krick" <wkrick at eio-online.com>
        Sent by: jdom-interest-admin at jdom.org
        15/07/2003 15:07


                To:        <jdom-interest at jdom.org>
                cc:
                Subject:        [jdom-interest] new element methods?



  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
  _______________________________________________
  To control your jdom-interest membership:

http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20030715/3b7a6e5f/attachment.htm


More information about the jdom-interest mailing list