帮忙:xjc抛出“ObjectFactory类中有两个声明导致冲突”的错误。

7

请看下面简化的XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="com.acme" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Widget">
        <xs:complexType>
            <xs:sequence>
                <xs:element 
                    minOccurs="0" name="color" nillable="true" type="xs:string" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="WidgetColor" type="xs:string" />
</xs:schema>

然后,尝试以下操作:
xjc test.xsd

您应该始终获取以下异常信息:
parsing a schema...
compiling a schema...
[ERROR] Two declarations cause a collision in the ObjectFactory class.
  line 11 of file:/C:/test.xsd

[ERROR] (Related to above error) This is the other declaration.
  line 7 of file:/C:/test.xsd

Failed to produce code.

请注意,有一个名为“Widget”的元素,它是一个complexType,并且具有名为“color”的元素。同时,在与“Widget”元素相同的级别上,还有一个简单元素命名为“WidgetColor”。
更令人困惑的是,如果您从“color”元素序列中删除minOccurs =“0”属性将nillable =“true”属性从中删除,则xjc可以成功编译模式。
有人遇到过这个问题或者可以提出解决方案吗?
谢谢,
迈克。
1个回答

7
我终于找到了解决问题的方法。问题出在使用自定义绑定来指定一个声明的不同类名上。
custom-binding.xjb 的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<bindings version="2.0" xmlns="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <bindings schemaLocation="test.xsd">
        <bindings node="//xs:element[@name='WidgetColor']">
            <class name="BaseWidgetColor" />
        </bindings>
    </bindings>
</bindings>

操作:

C:\>xjc -b custom-binding.xjb test.xsd
parsing a schema...
compiling a schema...
acme\com\BaseWidgetColor.java
acme\com\ObjectFactory.java
acme\com\Widget.java
acme\com\package-info.java

"耐心和时间比愤怒和固执更有价值...!"这句话意思是说,在做事情时要有耐心,不要心急,经过一段时间的努力,最终的成果会更好。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接