aspose file tools*
The moose likes XML and Related Technologies and the fly likes Customizing JAXB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Customizing JAXB" Watch "Customizing JAXB" New topic
Author

Customizing JAXB

Nicole Gustavson
Greenhorn

Joined: Jan 06, 2003
Posts: 12
I'm very new to JAXB and would like to know if the following is possible. I have a schema which is already approved and in use, and would like to use JAXB to generate some data classes. I'm getting a bunch of classes that I would like to be attributes instead. Is it possible to get them as simple attributes instead of separate classes?

Here is an excerpt of the XML schema:



Is it possible to get Gender as just a String member in the Person class, instead of a separate Gender class?

Is it possible to do it without changing the input format of the XML file?



Thanks very much.
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 378
Hi Nicole,
I googled for "jaxb classes schema style" and got this link..

which says..

XML Schema styles are based on various combinations of global and local scopes for XML Schema Element and Complex Type declarations. Table 1 shows the four styles.

Why is one style chosen over another? Normally, a developer may choose to use Style 1 <L, L> over Style 3 <L, G> simply due to level of knowledge and experience, lack of time, or simply a preference. However, each style will have different degrees of influence on the qualities of the XML Schema, and the resulting generated Java code. For example, maintainability of an XML Schema file will be significantly impacted based on what style is used, and comprehensibility of the resulting Java code will be sensitive to each style as well.



You might be able to tailor your java classes by changing the schema style. Apart from this I am unable to help you with this. There may be other solutions..

Hope this helps..
Nicole Gustavson
Greenhorn

Joined: Jan 06, 2003
Posts: 12
Thanks for the link, it was educational! It seems like there's no way to do what we want to do using JAXB. If you want to generate a class with properties, your XML schema has to have an element with attributes, not an element with subelements.

So, our choices are: change the XML schema to use attributes instead of subelements (may not be politically possible), or not use JAXB. It seems like we may have to drop JAXB.

So are there any alternatives other than writing our own data classes and XML parser using DOM? Can XSLT do anything for us?

Thanks again.
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 378
There is an element of risk involved in this.

What do you think of changing the schema just for the sake of getting the relevant classed generated? Now I haven't tried JAXB and don't know whether this'll work.

For other approaches, what are you trying to achieve exactly?
Nicole Gustavson
Greenhorn

Joined: Jan 06, 2003
Posts: 12
So it turns out the problem with the schema was that JAXB generates a set of classes for every top-level element and top-level complex type. XMLSPY (which generated our XML schema) creates a top-level element for every tag. So the solution was to change any element that doesn't require its own class into either a nested element, or a top-level simple type. Either one of these does the trick:
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 378
So you've solved your problem?
Nicole Gustavson
Greenhorn

Joined: Jan 06, 2003
Posts: 12
Yes.
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 378
great! :thumb:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Customizing JAXB