[jdom-interest] converting Elements to objects
Jason Hunter
jhunter at xquery.com
Thu Feb 17 12:38:08 PST 2005
The most elegant way I've seen to do this is JATO, but it's not a very
active project.
http://sourceforge.net/projects/jato
It was written up in JavaWorld in the past.
-jh-
Anton Stoyanov wrote:
> Hi,
>
> thanks for the answers :) it's my first post in this list and i'm
> really happy to get such instant expert opinions :)
>
>
> you are right I don't want to change the customer object in any way
> (extending element or taking in an element)
>
> 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.
>
>
> thanks alot
> anton
>
>
> On Thu, 17 Feb 2005 14:44:36 -0500, Jason Winnebeck <gillius at gillius.org> wrote:
>
>>Anton Stoyanov wrote:
>>
>>>with JDOM I can get a java.util.List of elements, but how can I
>>>transform them into customer objects ?
>>
>>As far as I know, there is not a JDOM method or another automatic method to
>>do this.
>>
>>The way I resolve this is by making a constructor for Customer that takes an
>>Element pointing to a customer tag, then reads the children of that tag.
>>
>>Element e = parent.getChild( "customer" );
>>Customer c = new Customer( e );
>>
>>If you want to separate the JDOM/XML parsing away from the design of the
>>Customer class (there are various reasons you may object to putting JDOM
>>code into the Customer class, espically if you did not write the Customer
>>class), you could use a sort of factory.
>>
>>If your XML is pretty structured and you aren't sure what you are going to
>>get out of it you could use a generic factory that returned an object
>>implementing an interface, and the specific type is dependent on the tag
>>passed in. Or, if you do know what you are getting, then you could do
>>something like:
>>
>>Element e = parent.getChild( "customer" );
>>Customer c = XMLObjectFactory.makeCustomer( e );
>>
>>makeCustomer would presumably read out the fields and call a verbose form of
>>constructor for Customer to construct an appropriate new Customer object.
>>
>>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