Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Customizing JAXB

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you've solved your problem?
 
Nicole Gustavson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
great! :thumb:
 
Evildoers! Eat my justice! And this tiny ad's justice too!
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic