You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Mark Wutka <ma...@wutka.com> on 2000/06/01 21:22:11 UTC

Bean->XML, XML->Bean

I don't know if this is helpful, but I wrote some classes to map XML to a
Bean and vice-versa.
It looks for XML tags that match Bean attribute names and does the
conversions as best it can. It also removes dashes, underscores and does
case-free compares to find a match, so <first-name> can be used to set a
bean attribute of firstName. Also, when spitting out XML from a bean, it can
use a DTD (my DTD parser is a joke) to figure out the format for the XML
tag, so again, firstName gets written to the XML file as <first-name> if the
DTD says so.

Also, it can handle nested object. If it sees a tag of <person>, for
example, and the bean has a property of person that is an Object (but not a
Date or a String), it attempts to create a new instance of that object and
populate it with the data contained in the <person> tag.

I was looking at the BeanUtils and I didn't think it did quite the same
thing. Is that something that might be useful?
    Mark


Re: Bean->XML, XML->Bean

Posted by Mark Wutka <ma...@wutka.com>.
----- Original Message -----
From: "Craig R. McClanahan" <Cr...@eng.sun.com>
To: <st...@jakarta.apache.org>
Sent: Thursday, June 01, 2000 3:41 PM
Subject: Re: Bean->XML, XML->Bean


> One of the many places something like this might be very useful is in
generating
> the Java source code for form beans, which are usually very simple but are
> tedious to write.  You could start from the same XML file and generate a
> "regular" JavaBean as well (which would, among other things, store nulls
> directly instead of translating them to zero-length strings).  I would
> definitely be interested in this code.

Okay, you can get it from http://www.wutka.com/download/jox.zip, or I can
e-mail it to you if you like. I also have a quick hack I called Beanmaker
(http://www.wutka.com/download/beanmaker.zip). It parses an XML file to
create a bean, even does indexed properties, but the format of the XML file
is more of a bean description than a data description. I'm not sure how I
would generate a bean from XML given that I don't have data types.

>
> You might also be interested in the "Digester" portion of Struts, which
can read
> an XML file and perform rules-based processing on it.  I used it in a very
> simple fashion to configure the action mappings in the controller servlet
(see
> the configure() method of org.apache.struts.action.ActionServlet), but you
can
> do other interesting things as well.
>
> Craig

I've been looking over the code for a few hours, looks pretty cool. I'd love
to help out somehow.
    Mark


Re: Bean->XML, XML->Bean

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Mark Wutka wrote:

> I don't know if this is helpful, but I wrote some classes to map XML to a
> Bean and vice-versa.
> It looks for XML tags that match Bean attribute names and does the
> conversions as best it can. It also removes dashes, underscores and does
> case-free compares to find a match, so <first-name> can be used to set a
> bean attribute of firstName. Also, when spitting out XML from a bean, it can
> use a DTD (my DTD parser is a joke) to figure out the format for the XML
> tag, so again, firstName gets written to the XML file as <first-name> if the
> DTD says so.
>
> Also, it can handle nested object. If it sees a tag of <person>, for
> example, and the bean has a property of person that is an Object (but not a
> Date or a String), it attempts to create a new instance of that object and
> populate it with the data contained in the <person> tag.
>
> I was looking at the BeanUtils and I didn't think it did quite the same
> thing. Is that something that might be useful?
>

One of the many places something like this might be very useful is in generating
the Java source code for form beans, which are usually very simple but are
tedious to write.  You could start from the same XML file and generate a
"regular" JavaBean as well (which would, among other things, store nulls
directly instead of translating them to zero-length strings).  I would
definitely be interested in this code.

You might also be interested in the "Digester" portion of Struts, which can read
an XML file and perform rules-based processing on it.  I used it in a very
simple fashion to configure the action mappings in the controller servlet (see
the configure() method of org.apache.struts.action.ActionServlet), but you can
do other interesting things as well.

>     Mark

Craig