[jdom-interest] new element methods?

Phill_Perryman at Mitel.COM Phill_Perryman at Mitel.COM
Tue Jul 15 09:59:14 PDT 2003


As the project was referred to as mpt i used MptElement and MptDocument. I 
put them into the com.company.mpt package.

You also have to create your own JDOMFactory (see below) so that the 
builder returns the correct classes and away you go.

Also because I am working with a document with the same tags and 
attributes all the time  I have added many methods to retrieve certain 
values
so rather than call getAttributeValue("Weight") i call 
getWeightAttributeValue() so I can isolate the names of the attributes in 
one place
so the maintenance becomes easier. I also created a static build method in 
MptDocument which uses the correct factory so I can call
MptDocument.build instead of  SAXBuilder.build

I have tried some of the parse methods but the problem always seems to 
come back to knowing how it was stored so you can parse it back, add to 
that the use of xslt on the stored file and it all gets horribly 
complicated.

/**
 * A JDOM Factory class for creating Elements and Document
 * used in parsing an xml document
 */
public class MptJDOMFactory extends DefaultJDOMFactory {


        /**
         * Constructor required by factory
         */
        public MptJDOMFactory() {
                super();
        }

        /**
         * Creates an element
         */
        public Element element(String name) {
                return MptElement.newMptElement(name);
        }

        /**
         * Create the Document object
         */
        public Document document(Element e) {
                return new MptDocument(e);
        }
}

/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 16:51

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


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@yourhost.com



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


More information about the jdom-interest mailing list