java 使用 xjc 和绑定为常见 XSD 定义包名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10741358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Defining package names for common XSD's with xjc and bindings
提问by Xavi López
I'm trying to generate Java classes from several specific XSD's with xjc
. Those schemas have some definitions in common, so they import a number of common XSD's. In particular, they can include from zero to all of the common XSD's.
我正在尝试从几个特定的 XSD 生成 Java 类xjc
。这些模式有一些共同的定义,因此它们导入了许多常见的 XSD。特别是,它们可以包括从零到所有常见的 XSD。
I'd like to generate all classes from an specific XSD to a specific package, but keeping the generated classes for the common schemas in a common package, so they don't get repeated for each specific schema in the source tree.
我想将所有类从特定 XSD 生成到特定包,但将公共模式的生成类保留在公共包中,因此它们不会为源树中的每个特定模式重复。
I've learnt that custom bindings can be used to specify packages on a per-schema basis, with for instance:
我了解到自定义绑定可用于在每个模式的基础上指定包,例如:
<jxb:bindings schemaLocation="common1.xsd" node="/xsd:schema">
<jxb:schemaBindings>
<jxb:package name="mypackage.commonclasses"/>
</jxb:schemaBindings>
</jxb:bindings>
I've got the following structure:
我有以下结构:
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
It all works fine with:
一切正常:
xjc -b schemas/specific1
-b schemas/common
schemas/specific1/specific1.xsd
It generates the classes for specific1.xsd
in mypackage.specific1
and the common classes in mypackage.commons
. But when I try to generate the classes for specificN
, xjc
throws the following error:
它生成specific1.xsd
in的类和inmypackage.specific1
的公共类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
I get this error repeated for every common XSD not imported in any specific xsd.
对于未在任何特定 xsd 中导入的每个常见 XSD,我都会重复此错误。
Is there any way I can make xjc
ignore the bindings in commonBindings.xjb
that aren't used in the XSD I'm generating the classes for?
有什么方法可以让我xjc
忽略在commonBindings.xjb
我生成类的 XSD 中没有使用的绑定?
Or, am I aiming in the wrong direction by using this approach, and should be, for instance, using annotations in the specific xsd? I'd like to avoid modifying the schemas if possible.
或者,我是否使用这种方法瞄准了错误的方向,例如,应该在特定的 xsd 中使用注释?如果可能,我想避免修改模式。
采纳答案by Petru Gardea
I think that what you need is to use the JAXB episode. See this SO post, take a look at the answer @BlaiseDoughan provided.
我认为您需要的是使用 JAXB 插曲。看到这个 SO 帖子,看看@BlaiseDoughan 提供的答案。
In your case, run xjc first, using your custom binding file for package name customization, against all the XSDs that are common, to generate an episode file and your common Java classes.
在您的情况下,首先运行 xjc,使用您的自定义绑定文件进行包名称自定义,针对所有常见的 XSD,生成一个情节文件和您的常见 Java 类。
Then use xjc again, with the other XSDs you want, by referencing the episode file from the first run.
然后通过引用第一次运行的剧集文件,再次使用 xjc,以及您想要的其他 XSD。