【问题标题】:xjc: Two declarations cause a collision in the ObjectFactory classxjc:两个声明导致 ObjectFactory 类中的冲突
【发布时间】:2012-11-05 12:18:18
【问题描述】:

运行以下 xjc 命令会引发错误:

$ xjc "ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd"
parsing a schema...
compiling a schema...
[ERROR] Two declarations cause a collision in the ObjectFactory class.
  line 340 of ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd

[ERROR] (Related to above error) This is the other declaration.   
  line 475 of ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd

虽然我了解 JAXB 绑定以及 XJC 中的冲突是什么,但我不明白当前架构中的冲突在哪里。

我应该如何解决这个问题?

谢谢,

皮埃尔

更新:这是错误的上下文:

$ curl -s "ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd" | sed 's/^[ \t]*//' | cat -n | egrep -w -A 10 -B 10 '(340|475)' 
   330  <xs:element maxOccurs="1" name="Description"
   331  type="xs:string" minOccurs="0">
   332  <xs:annotation>
   333  <xs:documentation>
   334  Optionally provide description especially when "eOther" is selected
   335  </xs:documentation>
   336  </xs:annotation>
   337  </xs:element>
   338  <xs:element name="BioSampleSet" minOccurs="0" maxOccurs="1"><xs:annotation><xs:documentation>Identifier of the BioSample when known</xs:documentation>
   339  </xs:annotation>
   340  <xs:complexType><xs:sequence><xs:element name="ID" maxOccurs="unbounded" type="xs:token"></xs:element>
   341  </xs:sequence>
   342  </xs:complexType>
   343  </xs:element>
   344  </xs:sequence>
   345  <xs:attribute name="sample_scope" use="required">
   346  <xs:annotation>
   347  <xs:documentation>
   348  The scope and purity of the biological sample used for the study
   349  </xs:documentation>
   350  </xs:annotation>
--
   465  <xs:documentation>Please,  fill Description element when choose "eOther"</xs:documentation>
   466  </xs:annotation>
   467  </xs:enumeration>
   468  </xs:restriction>
   469  </xs:simpleType>
   470  </xs:attribute>
   471  </xs:complexType>
   472  </xs:element>
   473  <xs:element name="TargetBioSampleSet">
   474  <xs:annotation><xs:documentation>Set of Targets references to BioSamples</xs:documentation></xs:annotation>
   475  <xs:complexType>
   476  <xs:sequence>
   477  <xs:element name="ID" type="xs:token" minOccurs="1" maxOccurs="unbounded"></xs:element>                                                 
   478  </xs:sequence>
   479  </xs:complexType>
   480  </xs:element>                        
   481  </xs:choice>
   482  <xs:element name="Method" minOccurs="1">
   483  <xs:annotation>
   484  <xs:documentation>
   485  The core experimental approach used to obtain the data that is submitted to archival databases

【问题讨论】:

  • 如果您不提供 Core.xsd 中的相关部分,我认为没有人可以帮助您
  • 也可以尝试使用较新的 Java JDK 来生成它们。在我在 JDK 1.8 中使用 xjc 之前,我在使用 JDK 1.7 时遇到了很多重复的错误。此外,如果有人试图通过右键单击 > 在 Eclipse 中生成,请确保也从命令行尝试它。这样做为我解决了错误。

标签: java binding xsd schema xjc


【解决方案1】:

我会引用网络上的the most official unofficial guide on JAXB

当模式包含相似的元素/类型名称时,它们可以 导致“两个声明导致 ObjectFactory 中的冲突 类”错误。更准确地说,对于所有类型和许多 元素(究竟什么元素得到了工厂,什么没有 很难解释),XJC 在 ObjectFactory 类上产生了一种方法 在同一个包中。 ObjectFactory 类是为每个 XJC 生成一些文件的包。该方法的名称是 派生自 XML 元素/类型名称,如果有两个则报告错误 元素/类型尝试生成相同的方法名称。

也就是说,你有两个选择。

首先是像这样定义一个外部绑定XML

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  version="1.0">
  <jaxb:bindings schemaLocation="Core.xsd">
    <jaxb:bindings node="//xs:element[@name='BioSampleSet']/xs:complexType">
      <jaxb:factoryMethod name="TypeBioSampleSet"/>
    </jaxb:bindings>
    <jaxb:bindings node="//xs:element[@name='TargetBioSampleSet']/xs:complexType">
      <jaxb:factoryMethod name="TypeTargetBioSampleSet"/>
    </jaxb:bindings>
  </jaxb:bindings>
</jaxb:bindings>

在生成的ObjectFactory 类中,这将创建两个称为createTypeBioSampleSetcreateTypeTargetBioSampleSet 的方法(JAXB 会将您指定的名称附加到单词create)可用于生成BioSampleSet 和@987654330 @对象。

(不必为两种类型定义绑定。)

我不确定为什么 JAXB 拒绝从给定模式生成类,但是当我只指定一个绑定(例如 BioSampleSet)时,另一种类型的工厂方法被命名为 createTypeProjectProjectTypeSubmissionWhateverThisAndThatTargetTargetSampleBioCatDogWoofTypeIDoNotKnowWhatElse 所以我认为JAXB 对这个长方法标识符感到窒息,因为它以某种方式设法为两种类型创建了相同的标识符。我认为这是 JAXB 中的一些实现细节。

另一种解决方案是为 BioSampleSet 创建一个基本类型,并在两个位置都使用它

<xs:element name="ProjectTypeSubmission">

...

  <xs:element name="Target">

    ...

    <xs:element name="BioSampleSet" type="typeBioSampleSet" minOccurs="0" maxOccurs="1"/>

    ...

  </xs:element>

  ...

  <xs:element name="TargetBioSampleSet" type="typeBioSampleSet"/>

  ...

<xs:element/>

...

<xs:complexType name="typeBioSampleSet">
  <xs:sequence>
    <xs:element name="ID" maxOccurs="unbounded" type="xs:token"></xs:element>
  </xs:sequence>
</xs:complexType>

最好的解决方案是从架构中删除所有匿名类型声明。如果你能做到,那就去做吧,因为这个架构看起来很乱(至少对我来说)。

【讨论】:

【解决方案2】:

删除命令中的-p包

xjc -d src -XautoNameResolution TFMData_Service.xsd

【讨论】:

  • 这是最简单的解决方案。尽管使用了 -XautoNameResolution,但无法弄清楚为什么我仍然会遇到冲突。删除包选项是关键。
【解决方案3】:

我遇到了同样的问题,对象工厂的冲突,我的解决方案是使用以下绑定:

<jaxb:bindings version="1.0"
               xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
               jaxb:extensionBindingPrefixes="xjc">

    <jaxb:bindings
            schemaLocation="../pfel_xsd/pfel-mng-managedocument-v4-vs_data.xsd"
            node="/xs:schema">
        <jaxb:schemaBindings>
            <jaxb:package name="document.client.pfelservice"/>
        </jaxb:schemaBindings>
    </jaxb:bindings>

    <jaxb:bindings
            schemaLocation="../pfel_xsd/pfel-mng-managedocument-v4-vs_data-types.xsd"
            node="/xs:schema">
        <jaxb:schemaBindings>
            <jaxb:package name="document.client.pfel"/>
        </jaxb:schemaBindings>
    </jaxb:bindings>

</jaxb:bindings>

这里是解决方案:https://www.javaer101.com/en/article/1278220.html

【讨论】:

    【解决方案4】:

    使用以下 maven 插件时对我有用的解决方案是添加 -XautoNameResolution

    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    

    示例代码:

           <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>2.5.0</version>
                <executions>
                    <execution>
                        <id>xjc</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <xjbSources>
                        <xjbSource>src/main/resources/global.xjb</xjbSource>
                    </xjbSources>
                    <sources>
                        <source>src/main/xsd/</source>
                    </sources>
                    <arguments>
                        <argument>-XautoNameResolution</argument>
                    </arguments>
                    <packageName>com.xxx.yyy.zzz.policy.xml</packageName>
                </configuration>
            </plugin>
    

    【讨论】:

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