【问题标题】:help please: xjc throws "Two declarations cause a collision in the ObjectFactory class"请帮助:xjc 抛出“两个声明导致 ObjectFactory 类发生冲突”
【发布时间】:2011-11-21 01:47:25
【问题描述】:

在简化的 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”的元素,它是一个复杂类型,并具有名为“color”的元素。在与元素“Widget”相同的级别上,还有一个名为“WidgetColor”的简单元素。

更令人费解的是,如果你删除属性 minOccurs="0" OR删除 "color" 元素序列中的属性 nillable="true",xjc 成功编译模式。

有没有人见过这个问题或者可以提出解决方案?

谢谢,

迈克。

【问题讨论】:

    标签: web-services jaxb jax-ws wsimport xjc


    【解决方案1】:

    嗯,我终于想出了如何解决我的问题。它在于使用自定义绑定为其中一个声明指定不同的类名。

    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
    

    Patience et longueur de temps valent mieux que rage et acharnement...!

    【讨论】:

      猜你喜欢
      • 2012-11-05
      • 2016-01-06
      • 2019-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-04
      • 2012-08-06
      相关资源
      最近更新 更多