【发布时间】:2013-08-14 04:17:05
【问题描述】:
我有多个 XSD 文件,我想在它们上运行 XJC 以创建 java 类对象,我将其用于进一步处理。 问题: 当我运行 XJC 命令时,我的一组 XSD 中有 2 个 XSD 出现问题。 XSD 是:
contactLM.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:w="http://w.com/xsd"
xmlns="http://w.com/location.contactlm"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
targetNamespace="http://w.com/location.contactlm"
elementFormDefault="qualified" jaxb:version="2.1">
<xsd:annotation>
<xsd:appinfo>
<jaxb:schemaBindings>
<jaxb:package name="location.contactlm"/>
</jaxb:schemaBindings>
</xsd:appinfo>
</xsd:annotation>
<xsd:element name="Contact" type="Contact" nillable="true"/>
<xsd:complexType name="Contact">
<xsd:sequence>
<xsd:element name="RefID" type="xsd:string" nillable="true"
minOccurs="0" gw:type="java.lang.String"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
contactM.xsd
<?xml version="1.0"?>
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:w="http://w.com/xsd"
xmlns="http://w.com/location.contactm"
targetNamespace="http://w.com/location.contactm"
elementFormDefault="qualified"
xmlns:ns0="http://w.com/location.contactam"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.1">
<xsd:annotation>
<xsd:appinfo>
<jaxb:schemaBindings>
<jaxb:package name="location.contactm"/>
</jaxb:schemaBindings>
</xsd:appinfo>
</xsd:annotation>
<xsd:import namespace="http://w.com/location.contactam"
schemaLocation="ContactAM.xsd"/>
<xsd:element name="Contact" type="Contact" nillable="true"/>
<xsd:complexType name="Contact">
<xsd:sequence>
<xsd:element name="RefID" minOccurs="0" nillable="true"
type="xsd:string" gw:type="java.lang.String"/>
<xsd:element name="WorkPhone" minOccurs="0" nillable="true"
type="xsd:string" gw:type="java.lang.String"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
我知道在这些 XSD 文件中引用了相同的“联系人”,这是一个问题。所以,我不得不写我的 XJB 文件。 我写了以下 xjb 文件:
<jaxb:bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:gw="http://w.com/xsd"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
xmlns="http://w.com/location.contactlm"
targetNamespace="http://w.com/location.contactlm"
jaxb:version="2.1">
<jaxb:bindings schemaLocation="ContactM.xsd">
<jaxb:bindings node=".//xs:element[@name='Contact']">
<jaxb:class name="ContactM"/>
</jaxb:bindings>
<jaxb:schemaBindings>
<jaxb:package name="location.contactm" />
</jaxb:schemaBindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="ContactLM.xsd">
<jaxb:bindings node=".//xs:element[@name='Contact']">
<jaxb:class name="ContactCLM"/>
</jaxb:bindings>
<jaxb:schemaBindings>
<jaxb:package name="location.contactlm" />
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
但是当我这样做时出现以下错误:
xjc -d src -b C:\us\binding.xjb C:\us\trunk\shared\
parsing a schema...
346368376 / 346685920 (-1 / -1) (com/sun/tools/internal/xjc/reader/xmlschema/bindinfo/CollectionTypeAttribute$JaxbAccessorF_collectionType/com/sun/tools/internal/xjc/reader/xmlschema/bindinfo/CollectionTyp
eAttribute$JaxbAccessorF_collectionType)
[ERROR] Multiple <schemaBindings> are defined for the target namespace "http://w.com/location.contactm"
line 10 of file:/C:/us/trunk/contact/ContactM.xsd
[ERROR] Another <schemaBindings> is defined here
line 14 of file:/C:/us/trunk/contact/binding.xjb
[ERROR] Multiple <schemaBindings> are defined for the target namespace "http://w.com//trunk/contactlm"
line 5 of file:/C:/us/trunk/contact/ContactLM.xsd
[ERROR] Another <schemaBindings> is defined here
line 23 of file:/C:/us/trunk/contact/binding.xjb
[ERROR] compiler was unable to honor this schemaBinding customization. It is attached to a wrong place, or its inconsistent with other bindings.
line 14 of file:/C:/us/trunk/contact/binding.xjb
[ERROR] (the above customization is attached to the following location in the schema)
line 10 of file:/C:/us/trunk/contact/ContactM.xsd
[ERROR] compiler was unable to honor this schemaBinding customization. It is attached to a wrong place, or its inconsistent with other bindings.
line 23 of file:/C:/us/trunk/contact/binding.xjb
[ERROR] (the above customization is attached to the following location in the schema)
line 2 of file:/C:/us/trunk/contact/ContactLM.xsd
Failed to parse a schema.
我想我可能在 XJB 文件中做错了什么,但我无法解决这个问题。有人请帮忙。
【问题讨论】:
-
您的前两条错误消息表明 Jaxb 正在为您的两个命名空间中的每一个找到两个 schemaBindings 元素——一个在 xjb 文件中,一个在 XSD 模式文档中。问问自己(并询问文档,并在紧要关头询问 JAXB 编译器):我真的需要说两次吗?还是一次就够了?
-
@C.M.Sperberg-McQueen 感谢您的回复。我很担心为什么没有人回应! :-( 至于,我是否需要两个架构文档,是的,因为,我在 xjb 文件中编写的那个覆盖了 XSD 架构文档。至少,这是我从阅读手册中了解到的。如果这是,请告诉我错了。
-
我的另一个问题是,我的 XJB 文件有误吗?我从互联网上的各种来源收集到的所有知识组成了我的 xjb 文件。换句话说,它是来自各种来源的大杂烩。我感觉文件中可能有问题。你有什么要说的吗?
-
我不是 JAXB 专家;我只能给你一般的建议。尝试不同的配置。首先制作最简单的测试用例,以生成此错误消息。 (一旦你理解了问题,修复你的真实设置就很容易了。)你的任务是修复一个错误;像处理 Java 中的错误一样系统地工作。 JaxB 发现冲突;尝试找到并解决它。尝试抑制内联注释,然后是外部注释; JaxB 开心吗?抑制
jaxb:package元素(内联或外部);然后抑制jaxb:schemaBindings。祝你好运。