Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

generate java bean from an xml file?

Thread view: 
Elhanan - 14 Sep 2006 15:46 GMT
hi..

first don't say xsl, it has some drawbacks (like being too complex) i'
was wondering if there is some infrastucture like an object model or
something like it that can generate javabean code (i have a custom xml
file which i need to use for the source creation).
Daniel Dyer - 14 Sep 2006 15:52 GMT
> hi..
>
> first don't say xsl, it has some drawbacks (like being too complex) i'
> was wondering if there is some infrastucture like an object model or
> something like it that can generate javabean code (i have a custom xml
> file which i need to use for the source creation).

There are dozens of such tools.  http://bindmark.dev.java.net/ provides  
benchmarks and links for many.  JiBX (http://www.jibx.org) is my current  
favourite.

Dan.

Signature

Daniel Dyer
http://www.dandyer.co.uk

su_dang@hotmail.com - 14 Sep 2006 15:52 GMT
> hi..
>
> first don't say xsl, it has some drawbacks (like being too complex) i'
> was wondering if there is some infrastucture like an object model or
> something like it that can generate javabean code (i have a custom xml
> file which i need to use for the source creation).

I think JAXB is created for that purpose.  See this link

http://www-128.ibm.com/developerworks/edu/x-dw-xjaxb-i.html
Elhanan - 15 Sep 2006 06:58 GMT
you are talking about xml data binding, that's exactly what i want to
avoid,i don't have an xsd, and creating one from the custom, is rather
complex,
the beans i'm want to generate

on top of that, not many xml data-binding tools support what i'm
looking for (i thought about at first), in fact jaxb was the only one
came close (it supports id, idref, by actually getting objectt
references, as well substition groups) all others either don't support
all of it, or have known bug suppurting it.
however jaxb , it too much java like, it forces you to use factories,
to create the beans, as welll other probems (like inheritence).

> > hi..
> >
[quoted text clipped - 6 lines]
>
> http://www-128.ibm.com/developerworks/edu/x-dw-xjaxb-i.html
ozgwei - 15 Sep 2006 07:27 GMT
> you are talking about xml data binding, that's exactly what i want to
> avoid,i don't have an xsd, and creating one from the custom, is rather
> complex,
>  the beans i'm want to generate

You may want to try xstream from codehaus.org.

However, it doesn't generate code, it just does binding according to
your rules specified in your code.

Another choice may be Jakarta Commons Bitwixt, similarly, no code
generation.

Personally, I don't like generated code because it's a maintenance
nightmare...
RiCaRdO - 15 Sep 2006 09:19 GMT
I may be missing the point here, but these two classes in the standard
JDK (since 1.4 anyway) are often overlooked, but I find them very
useful:

http://java.sun.com/j2se/1.5.0/docs/api/java/beans/XMLEncoder.html
and
http://java.sun.com/j2se/1.5.0/docs/api/java/beans/XMLDecoder.html

You don't have to define any xml schema, just use it like regular
object serialization.
The XML output is quite simple and readable too.
RiCaRdO - 15 Sep 2006 09:23 GMT
Oh. now that I've re-read that, yes I have missed the point...

sorry for the noise.

Is it arbitrary XML you are trying to generate class files for? or has
it been outputted from a particular tool?
Elhanan - 15 Sep 2006 12:58 GMT
the xml is generated from a closed product, i'm designing how the
output is going to be generated (and no , it won't be xsd), based on
that xml, i'm going to create an spring beans file, as well as classes
and interaces.

you see the closed tool is actually a java questionair generator which
can build html screens for online questionairs, but also can recive
data using xml. the end user can design the questionair, so i have to
adap with minmim change.

the fields are usually grouped in inside same subject elements, for
example you can have several fields called name, age, and weight, the
fact the these fields belong to an element called person, cannot be
change by the end user
however what can be changed is how these fields appear, validated and
displayed. such defintions, are located in something called source (not
source code)

so field called name would can have different defintions in two
seperate sources, when you start answering a questionair you select
which sources you want be used, based on that infomation the the
selected sources are in union thus creating a questionair.

so let's say you have
class Person {
 private String name
 public getName
 public setName;

 private int age
 public getAge
 public setAge;

 private int weight
 public getweight
 public setweight;

}

but you selected that only name and age would be in a source. number 1

so you get
interface PersonSource1{

private String name
 public getName
 public setName;

 private int age
 public getAge
 public setAge;
}

and have the person class implement that interface.

so if have like 20 sources, i'll have 20 interfaces generated for that
class

so xml i'm building is going to reflect that structure by saying (more
simplifed)

<Element Name="Person">
  <Attribute Name="Name>
       <Sources>
            </Source Id="1"/>
            </Source Id="2"/>
       </Sources>
  </Attribute
  <Attribute Name="Age>
       <Sources>
            </Source Id="1"/>
       </Sources>
  </Attribute
</Element>

the relationship between these elemetns is fixed so i won't have to
regenerate it each time.

> Oh. now that I've re-read that, yes I have missed the point...
>
> sorry for the noise.
>
> Is it arbitrary XML you are trying to generate class files for? or has
> it been outputted from a particular tool?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.