2012-11-16 32 views
17

运行以下XJC命令碰撞引发错误:XJC:两个声明引起ObjectFactory类

$ 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冲突,我不理解当前模式中的冲突在哪里。

我该如何解决这个问题?

感谢,

皮埃尔

更新:这里是错误的情况下:我会从the most official unofficial guide on JAXB在网络上引用

$ 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 
+0

我不认为任何人都可以帮助你,如果你不提供相关的核心部分。xsd – hoaz

回答

24

当架构包含容貌相似元素/类型名称,它们可以 结果“两个声明引起的ObjectFactory 类碰撞”的错误。更准确地说,对于所有类型和许多元素(具体来说哪些元素获得工厂,哪些元素不是位是 很难解释),XJC在同一个包中的ObjectFactory类 上生成一个方法。 ObjectFactory类是为XJC生成一些文件的每个 包创建的。该方法的名称是从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名称)的外部绑定的XML可用于生产BioSampleSetTargetBioSampleSet对象。

(这是没有必要定义两种类型的绑定。)

我不知道是什么原因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> 

最好的解决办法是每一个匿名类型声明从您的架构下降。如果你能做到这一点,就这样做吧,因为这个模式看起来像一团糟(至少对我来说)。

+0

“我不确定为什么JAXB拒绝从给定模式生成类”,我也不是,但非常感谢你,这很有效! :-) – Pierre

+0

@KohányiRóbert尼斯答案,它解决了我的问题。非常感谢! – user1516873

+0

感谢小费,只是想知道为什么我找不到架构http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/jaxb/bindingschema_2_0.xsd任何factoryMethod元素,虽然它在HTTPS文件:/ /jaxb.java.net/nonav/2.0/binding-customization/http.java.sun.com.xml.n/index.html – kasi