[jdom-interest] converting Elements to objects

Gregory S. Hill ghill at vmtllc.com
Thu Feb 17 17:57:46 PST 2005


Jason (et al),

I know almost nothing about Reflection, (other than it's supposed to be 
this really nifty-slick technology that solves all problems magically!) 
but would there be a way of using it to analyze the setters 
(transformers?  I get lost in the terminology sometimes) and 
automatically set the values according to the element names in the XML? 
  Am I completely talking out of my ass right now? :)

-greg

On Feb 17, 2005, at 5:27 PM, Jason Winnebeck wrote:

> Anton Stoyanov wrote:
>
> > Jason, you say makeCustomer would read out the fields..... can I make
> > this generic - so that it's not makeCustomer but "makeObject(Element
> > e)" which will just take in an element and automatically recognize if
> > it's a customer or saleitem or whatever object and make old the
> > children into strings, say, in that object.
>
>
> Yes, as I said in my first e-mail (without giving explicit code), I 
> did say you could do that, but it wouldn't be any easier.  Of course, 
> you can implement a method makeObject such that:
>
> public static Object makeObject( Element e ) {
>   if ( e.getName() == "customer" )
>     return makeCustomer( e );
>   else if ( e.getName() == "saleitem" )
>     return makeSaleItem( e );
> }
>
> You could also implement the following with a Map< String, 
> XMLObjectFactory > where you make XMLObjectFactory implementations for 
> each type of object.  XMLObjectFactory would be an interface with 
> method "Object makeObject( Element e)" then you would construct the 
> map thusly:
>
> map.put( "customer", new XMLCustomerFactory() );
>
> and use it thusly:
>
> return map.get( "customer" ).makeObject( e ); //error checking left out
>
> If you are thinking you are going to get automatic code that somehow 
> automagically relates XML tags to name fields and writes code for you, 
> you won't find it (at least not in the form you are thinking of).
>
> Jason Winnebeck
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com



More information about the jdom-interest mailing list