Java xjc:两个声明导致ObjectFactory类中发生冲突

Java xjc:两个声明导致ObjectFactory类中发生冲突,java,binding,xsd,schema,xjc,Java,Binding,Xsd,Schema,Xjc,运行以下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

运行以下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
$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
332
333
334可选地提供说明,尤其是在选择“eOther”时
335
336
337
338已知时生物样品的标识符
339
340
341
342
343
344
345
346
347
348用于研究的生物样品的范围和纯度
349
350
--
465请在选择“其他”时填写描述元素
466
467
468
469
470
471
472
473
474组生物样本的目标参考
475
476
477
478
479
480
481
482
483
484
485用于获取提交到档案数据库的数据的核心实验方法
我将在网上引用

当模式包含外观相似的元素/类型名称时,它们可以 导致“两个声明在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>
<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>

最好的解决方案是从模式中删除每个匿名类型声明。如果您可以这样做,那么就这样做,因为这个模式看起来很混乱(至少在我看来)。

在命令中删除-p包

xjc -d src -XautoNameResolution TFMData_Service.xsd

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

<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>


解决方案如下:

如果您不提供Core.xsdal中的相关部分,我认为任何人都无法帮助您。因此,请尝试使用较新的Java JDK生成它们。在JDK1.8中使用xjc之前,我在使用JDK1.7时有很多重复错误。另外,如果有人试图在Eclipse中通过右键单击>生成来生成,请确保也从命令行尝试。这样做为我解决了错误。“我不太清楚为什么JAXB拒绝从给定的模式生成类,”:我也不知道,但非常感谢,这很有效!:-)@Kohányi Róbert回答得不错,它解决了我的问题。谢谢!感谢您的提示,我只是想知道为什么我在模式中找不到任何factoryMethod元素,尽管它在最简单的解决方案中有文档记录。虽然使用了-XautoNameResolution,但无法理解为什么仍然会发生碰撞。删除包选项是关键。