简体   繁体   中英

Using JAXB to separate atributes from one class to multiple elements in XML

I'm still at a design phase, so I don't have any code snippets at the moment.

Context

I'm writing an application to compose relatively complex XML, with 3 main elements and about 25 parameters (mostly attributes and some values). There is quite a lot of custom types, so XJC generates about 15 classes (most of them are however strings or numbers with some added constraints). Edit: There is one root element, which contains these 3 elements, that I'm talking about.

The purpose of it is to allow the user to generate a large variety of different variants allowed by the schema. So I basically need to create a swing form and eventually create xml based on the inputted data.

It doesn't seems practical to attempt to somehow map 25 different fields in a form to a tree-like structure of objects (especially since many of the attributes are optional, so some of the object shouldn't even exist until the user inputs something).

So the best solution seems to be to save the data from the swing form to one bean (or a few) and then create some representation which JAXB will then marshal.

Question

Is there a way to convince JAXB to take that one bean and directly create 3 elements with attributes and values all taken from that one class?

Edit: After some discussion, different way of asking would be: Is there a way to flatten the object model, that xjc generated for me?

Since, you're already using XJC and your final object contains a root element with 3 main children elements (which in-turn have a complex structure). There's no convincing needed from JAXB, just find out the top level class generated in the XJC output and start using it directly in your resource method, marshalling & un-marshalling should happen out of the box.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM