JAXB xsd:包含和单独的包
我有以下情况:
有2个xsd文件。第一个定义了一个根元素和几个类型。
There are 2 xsd files. The 1st one defines a root element, and several types.
第二个包含第一个,并扩展其中一个类型。
没有在此文件中定义的根类型。
The second one includes the first, and extends one of the types. There is no root type defined in this file.
从第一个xsd开始,在包(a)中生成模型。
第二个模式应为其他
类型创建一个新包(b),但重用生成的包a。我通过使用
绑定文件来解决这个问题,该文件指向先前生成的元素(在
包a中)。
到目前为止,这是有效的,但是..
From the first xsd, a model is generated in a package (a). The second schema should create a new package (b) for the additional types, but reuse the generated package a. I solved this by using a binding file which points to the previously generated elements (in package a). So far this works, but..
JAXB在包A中生成一个ObjectFactory,它包含一个用于根元素的create
方法。
对于第二个模式,还在包B中创建了一个ObjectFactory.
此类还具有相同根元素的create方法。
JAXB generates a ObjectFactory in package A, which contains a create method for the root element. For the second schema, also an ObjectFactory is created in package B. And this class also had the create method for the same root element.
为了能够使用所有类型,使用
多个对象工厂创建jaxb上下文( newInstance(a.ObjectFactory.class,b.ObjectFactory.class)
)。
To be able to use all types, the jaxb context is created using
multiple object factories (newInstance(a.ObjectFactory.class, b.ObjectFactory.class)
).
在运行时,这会导致以下错误:
At runtime this results in the following error:
com。 sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:2
IllegalAnnotationExceptions计数
元素名称{http://www.example.org/Scenario/}scenario有更多
比一个映射
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions The element name {http://www.example.org/Scenario/}scenario has more than one mapping
我应该以不同方式生成包吗?或者是否有一些
可能使用绑定文件来防止
的对象工厂有重复的方法?
Should I generate the packages differently? Or is there something possible using the binding file to prevent the object factory from having duplicate methods?
首先,重要的是要理解,如果你使用 xsd:include
而不是 xsd:import
,你没有两个不同的模式。它是几个文件中的一个模式,并在几个包中进行编译,并且欺骗JAXB来组合这些包看起来更像是黑客。
First of all, it is important to understand that if you're using xsd:include
instead of xsd:import
, you don't have two different schemas. It's one schema in several files and compiling it in several packages and tricking JAXB to combine these packages looks more like hacking.
所以我的主要建议是使用 xsd:import
而是考虑分开模式编译方法。
So my primary suggestion would be to use xsd:import
instead and consider separate schema compilation approach.
如果你想留在 xsd:include
,你将拥有欺骗JAXB。例如,您可以删除或调整 ObjectFactory
类中的一个(或两个),并根据各个类而不是对象工厂构建JAXB上下文。您也可以使用 jaxb.index
而不是对象工厂。但这都是黑客攻击。
If you want to stay with xsd:include
, you'll have to trick JAXB. For instance, you can remove or tweak one (or both) of the ObjectFactory
classes and build you JAXB context based on individual classes rather than object factories. You can also use jaxb.index
instead of object factories. But it's all hacking.