简体   繁体   English

使用JAXB将属性从一个类分离到XML中的多个元素

[英]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). 我正在编写一个应用程序来组成相对复杂的XML,该XML具有3个主要元素和大约25个参数(主要是属性和某些值)。 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). 有很多自定义类型,因此XJC生成大约15个类(但是,大多数是带有某些附加约束的字符串或数字)。 Edit: There is one root element, which contains these 3 elements, that I'm talking about. 编辑:有一个根元素,其中包含我正在谈论的这3个元素。

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. 因此,我基本上需要创建一个摆动表单,并最终根据输入的数据创建xml。

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). 试图以某种方式将25个不同的字段映射到树状的对象结构似乎不切实际(特别是由于许多属性是可选的,因此在用户输入某些内容之前,某些对象甚至不应该存在) )。

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. 因此,最好的解决方案似乎是将数据从swing表单保存到一个(或几个)bean,然后创建某种表示形式,然后JAXB将将其封送。

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? 有没有一种方法可以说服JAXB接受那个bean,并直接创建3个元素,这些元素的属性和值都取自那个类?

Edit: After some discussion, different way of asking would be: Is there a way to flatten the object model, that xjc generated for me? 编辑:经过讨论,提出的不同方法是:是否有一种方法可以使xjc为我生成的对象模型扁平化?

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). 从那时起,您已经在使用XJC,并且您的最终对象包含一个带有3个主要子元素的根元素(这些子元素又具有复杂的结构)。 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. JAXB不需要令人信服,只需找出XJC输出中生成的顶级类,然后直接在您的资源方法中开始使用它,就应该开箱即用地进行编组和解组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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