使用xjc和绑定定义常见XSD的包名称

Xav*_*ica 8 java xml xsd jaxb xjc

我正在尝试从几个特定的​​XSD生成Java类xjc.这些模式有一些共同的定义,因此它们导入了许多常见的XSD.特别是,它们可以包括零到所有常见的XSD.

我想生成从特定XSD到特定包的所有类,但是将公共模式的生成类保存在公共包中,因此不会对源树中的每个特定模式重复这些类.

我已经了解到,自定义绑定可用于在每个模式的基础上指定包,例如:

<jxb:bindings schemaLocation="common1.xsd" node="/xsd:schema">
    <jxb:schemaBindings>
        <jxb:package name="mypackage.commonclasses"/>
    </jxb:schemaBindings>
</jxb:bindings>
Run Code Online (Sandbox Code Playgroud)

我有以下结构:

schemas
| - common
| | - common1.xsd --> XSD with common types #1
| | - ...
| | - commonN.xsd --> XSD with common types #N
| | - commonBindings.xjb --> Defines package "mypackage.commons" for common*.xsd
| - specific1
| | - specific1.xsd --> Includes ../common/common{1-N}.xsd
| | - specific1.xjb --> Defines package "mypackage.specific1" for specific1.xsd
| - specificN
| | - specificN.xsd --> Includes only ../common/common1.xsd
| | - specificN.xjb --> Defines package "mypackage.specificN" for specificN.xsd
Run Code Online (Sandbox Code Playgroud)

一切正常:

xjc -b schemas/specific1
    -b schemas/common 
    schemas/specific1/specific1.xsd
Run Code Online (Sandbox Code Playgroud)

它为specific1.xsdin mypackage.specific1和common公共类生成类mypackage.commons.但是当我尝试生成类时specificN,xjc会抛出以下错误:

[ERROR] "file:/drive/dir/schemas/common/common1.xsd" is not a part of
 this compilation. Is this a mistake for "/drive/dir/schemas/common/commonBindings.xjb"?
  line 2 of file:/drive/dir/schemas/common/commonBindings.xjb
Run Code Online (Sandbox Code Playgroud)

对于未在任何特定xsd中导入的每个常见XSD,我都会重复此错误.

有什么办法可以xjc忽略commonBindings.xjb我在生成类的XSD中没有使用的绑定吗?

或者,我是否通过使用这种方法瞄准了错误的方向,例如,应该在特定的xsd中使用注释?如果可能的话,我想避免修改模式.

Pet*_*dea 5

我认为您需要的是使用 JAXB 插曲。请参阅此 SO 帖子,看看@BlaiseDoughan 提供的答案。

在您的情况下,首先运行 xjc,使用您的自定义绑定文件进行包名称自定义,针对所有常见的 XSD,生成一个情节文件和您的常见 Java 类。

然后通过引用第一次运行的剧集文件,再次使用 xjc,以及您想要的其他 XSD。