问题: 我如何让 xjc/Jaxb 为同一命名空间中包含重复元素定义的多个模式生成适当的 java 类?
信息: 我有三个 .xsd 架构:A、B 和 C。它们都具有相同的目标 namespace 。 它们都是给我的 3 个 shemas,我不能以任何可能的方式以任何方式更改它们。
他们 A 有一些在 B 或 C 中也存在的元素(但 A 也有很多 self 声明的元素) 示例:这是 A 和 C 的相同“代码”:
<xs:simpleType name="y_ym_ymdDatoType">
<xs:union memberTypes="arcgYearType arcgYearMonthType arcDateType"/>
</xs:simpleType>
<xs:simpleType name="arcgYearType">
<xs:restriction base="xs:gYear">
<xs:minInclusive value="1700"/>
<xs:maxInclusive value="2100"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="arcgYearMonthType">
<xs:restriction base="xs:gYearMonth">
<xs:minInclusive value="1700-01"/>
<xs:maxInclusive value="2100-12"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="arcDateType">
<xs:restriction base="xs:date">
<xs:minInclusive value="1700-01-01"/>
<xs:maxInclusive value="2100-12-31"/>
</xs:restriction>
</xs:simpleType>
当使用 xjc 将它们编译成 javaclasses 时,出现以下异常:
[ERROR] 'y_ym_ymdDatoType' is already defined
line 297 of file:../c.xsd
[ERROR] (related to above error) the first definition appears here
line 309 of file:../a.xsd
其他元素也是如此:arcgYearType、arcgYearMonthType 和 arcDateType。
我读过一个可能可以解决这个问题的绑定(bind)文件,但我不确定如何去做,所以最好有例子。
最佳答案
根据您的描述,我假设 XSD 文件之间没有包含关系。此外,我必须假设您正在尝试重用内容重叠的类。
简单的方法是独立“编译”每个文件,并为每个 XSD 文件提供不同的 Java 包。这里的问题是,如果您尝试将内容从一个 XML“链接”到另一个 XML(即从 A 解码然后编码到 B),那么包 com.A 中的类 C1 和包 com.B 中的类 C1,而匹配相同的 XSD 复杂类型,不是“可互换的”;您将必须在它们之间进行转换。您需要自定义绑定(bind)文件,或者如果您使用 NetBeans,只需在 JAXB 向导中设置不同的包即可。
最好的方法可能是使用剧集(参见 this on SO )。处理A.xsd,然后用那个episode处理B.xsd等
关于java - 使用 JAXB 编译多个包含相同元素的重复定义的 XSD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9683907/