[jdom-interest] converting Elements to objects
Marco Ferretti
marco.ferretti at jrc.it
Fri Feb 18 00:53:31 PST 2005
On Thu, 2005-02-17 at 21:14 -0800, Tatu Saloranta wrote:
> --- "Gregory S. Hill" <ghill at vmtllc.com> wrote:
>
> > 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?
> > :)
>
> Nope -- using Reflection is the obvious way to create
> automated Object/XML bindings dynamically, along the
> lines you are thinking of. It can dynamically figure
> out available methods, and call them. There is some
> overhead in doing so (both regarding convenience of
> method calls and performance), but that's just part of
> the trade-off; it buys flexibility.
> So if you want to make a truly flexible system, that's
> one obvious way to go, and used by most existing
> binding system. Using Reflection API is bit cumbersome
> (since it was bolted on to JVMs afterwards, not an
> initial part of Java), but not all that complicated.
> Another bit more advanced alternative is to generate
> equivalent byte-code (optimally in run-time), usually
> based on Reflection-obtained or configuration file
> configuration.
>
> There are other ways to solve the problem, and there's
> often the compromise between writing/generating glue
> code (to get better static type safety, but do more
> monkey work one way or the other) and losing
> type-safety (using generic 'getString("author");',
> 'getInt("salary")' etc methods).
>
> -+ Tatu +-
>
>
>
>
> __________________________________
> Do you Yahoo!?
> Take Yahoo! Mail with you! Get it on your mobile phone.
> http://mobile.yahoo.com/maildemo
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com
I was just about to reply as you did ...
Anton, if you name the methods in Customer say setName and so on you can
just call the method by the child name (just capitalize the 1st char)
and instantiate the class by the element name ( again capitalize the 1st
char ) . Done in this way you have a simple "generic factory" for all of
the your objects stated that your xml files must at least conform :
1) the name of the element is the same of the class (apart for the 1st
char that can be lower case)
2) the name of the children must follow the same rule as the parent.
and your class must implement the empty contructor (for simplicity)
hope this helps
Marco
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.jdom.org/pipermail/jdom-interest/attachments/20050218/7531d08f/attachment.htm
More information about the jdom-interest
mailing list