2010-03-09 137 views
18

我使用jaxb從xml模式中生成java類。 模式導入XMLSchema.xsd,其內容用作文檔中的元素。使用JAXB從XMLSchema.xsd中生成Java類

如果我分別刪除導入和對「xsd:schema」的引用,則綁定編譯器會成功生成這些類。 如果我不這樣做,它會產生以下錯誤,如果我只嘗試從XMLSchema.xsd生成Java類,那麼這些錯誤是相同的!

> C:\Users\me>"%JAXB%/xjc" -extension -d tmp/uisocketdesc -p uis.jaxb uisocketdesc.xsd -b xml_binding_test.xml -b xml_binding_test_2.xml 
-b xml_binding_test_3.xml 
parsing a schema... 
compiling a schema... 

> [ERROR] A class/interface with the same name "uis.jaxb.ComplexType" is already in use. Use a class customization to resolve this conflict. 
line 612 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Relevant to above error) another "ComplexType" is generated from here. 
line 440 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] A class/interface with the same name "uis.jaxb.Attribute" is already in use. Use a class customization to resolve this conflict. 
line 364 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Relevant to above error) another "Attribute" is generated from here. 
line 1020 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] A class/interface with the same name "uis.jaxb.SimpleType" is already in use. Use a class customization to resolve this conflict. 
line 2278 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Relevant to above error) another "SimpleType" is generated from here. 
line 2222 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] A class/interface with the same name "uis.jaxb.Group" is already in use. Use a class customization to resolve this conflict. 
line 930 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Relevant to above error) another "Group" is generated from here. 
line 727 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] A class/interface with the same name "uis.jaxb.AttributeGroup" is already in use. Use a class customization to resolve this conflict. 
line 1062 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Relevant to above error) another "AttributeGroup" is generated from here. 
line 1026 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] A class/interface with the same name "uis.jaxb.Element" is already in use. Use a class customization to resolve this conflict. 
line 721 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Relevant to above error) another "Element" is generated from here. 
line 647 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] Two declarations cause a collision in the ObjectFactory class. 
line 1020 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Related to above error) This is the other declaration. 
line 364 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] Two declarations cause a collision in the ObjectFactory class. 
line 2278 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Related to above error) This is the other declaration. 
line 2222 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] Two declarations cause a collision in the ObjectFactory class. 
line 930 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Related to above error) This is the other declaration. 
line 727 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] Two declarations cause a collision in the ObjectFactory class. 
line 440 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Related to above error) This is the other declaration. 
line 612 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] Two declarations cause a collision in the ObjectFactory class. 
line 1026 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Related to above error) This is the other declaration. 
line 1062 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] Two declarations cause a collision in the ObjectFactory class. 
line 647 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Related to above error) This is the other declaration. 
line 721 of "http://www.w3.org/2001/XMLSchema.xsd" 

Failed to produce code. 

回答

4

看起來您的模式已損壞。 XJC 極其挑剔這些事情,並可能在其他工具通過的事情上失敗。

我發現通過首先運行AlphaWorks Schema Quality Checker來更容易地運行架構,而不是挑選XJC的錯誤。這給出了一個很好的,人類可讀的(好的,開發者可讀的,不管怎樣)的輸出。如果它能夠通過,那麼你也將有更好的機會通過XJC。

+1

感謝您對SQC的建議,儘管它不承認上述問題。 – david 2010-06-23 12:45:59

+0

該鏈接似乎被打破。你能提供一個新的嗎? – 2015-03-10 15:40:46

+0

我用https://www.wsdl-analyzer.com – 2016-05-02 03:59:52

-1

您可以使用JDK1.6附帶的xjc。您也可以嘗試XML到Java,請參閱this article

+0

OP已經在使用xjc – 2017-04-04 20:40:46

9

您可以通過(至少)兩種方法成功獲取JAXB以生成XML Schema xsd的代碼。您遇到的問題源於某些模式類型和元素共享相同名稱的事實。


第一個選項通過對生成的類名應用一個或多個XML名稱轉換來避免名稱衝突。下面是一個外部綁定文件,將做到這一點的例子:

<jxb:bindings version="2.1" 
       xmlns:jxb="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" 
       jxb:extensionBindingPrefixes="xjc"> 
    <jxb:globalBindings> 
    <xjc:simple/> 
    </jxb:globalBindings> 
    <jxb:bindings schemaLocation="XMLSchema.xsd"> 
    <jxb:schemaBindings> 
     <jxb:nameXmlTransform> 
     <jxb:elementName suffix="Element"/> 
     </jxb:nameXmlTransform> 
    </jxb:schemaBindings> 
    </jxb:bindings> 
</jxb:bindings> 

這種方法適用(你可以應用其他轉換,將工作一樣好,但我的文檔中遍地可見這種技術)但我認爲它會產生醜陋的結果。例如,在這種情況下生成的類名爲ElementElement


第二種方法使用xjc輸出建議的類定製。事實上,除了其中一個問題類以外,其他所有類都具有在相應模式類型中設置的屬性abstract="true"。因此,對於我來說,在「類別名稱」前面加上「抽象」即是有意義的,即AbstractElement。剩餘的Attribute類不是抽象類,但名爲attributexs:element生成一個空體的擴展類。所以我稱之爲BaseAttribute。下面是我使用的外部綁定定義:

<jxb:bindings version="2.1" 
       xmlns:jxb="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" 
       jxb:extensionBindingPrefixes="xjc"> 
    <jxb:globalBindings> 
    <xjc:simple/> 
    </jxb:globalBindings> 
    <jxb:bindings schemaLocation="XMLSchema.xsd"> 
    <jxb:schemaBindings> 
     <jxb:package name="org.w3.xmlschema"/> 
    </jxb:schemaBindings> 
    <jxb:bindings node="//xs:complexType[@name='complexType']"> 
     <jxb:class name="AbstractComplexType"/> 
    </jxb:bindings> 
    <jxb:bindings node="//xs:complexType[@name='group']"> 
     <jxb:class name="AbstractGroup"/> 
    </jxb:bindings> 
    <jxb:bindings node="//xs:complexType[@name='attributeGroup']"> 
     <jxb:class name="AbstractAttributeGroup"/> 
    </jxb:bindings> 
    <jxb:bindings node="//xs:complexType[@name='simpleType']"> 
     <jxb:class name="AbstractSimpleType"/> 
    </jxb:bindings> 
    <jxb:bindings node="//xs:complexType[@name='element']"> 
     <jxb:class name="AbstractElement"/> 
    </jxb:bindings> 
    <jxb:bindings node="//xs:complexType[@name='attribute']"> 
     <jxb:class name="BaseAttribute"/> 
    </jxb:bindings> 
    </jxb:bindings> 
</jxb:bindings> 

這讓我覺得更清晰生成的類名,併成功編譯架構。


我也建議使用單獨的編譯爲XMLSchema.xsd並保持它在的情況下,以後使用的jar文件這個問題又出來了,而編譯另一個模式。 this question的答案描述瞭如何。

+0

面臨類似的問題。但是,我有大約50個這樣的變量替代解決方案? – wib 2015-09-10 11:21:47

14

對付這樣的東西,對我來說,它是區分大小寫的問題和元素和屬性問題(通過繼承有時)相同的名稱。對於第二個問題,我使用的是含有這樣的外部綁定文件:

<jaxb:bindings node="//xs:complexType[@name='AbstractGriddedSurfaceType']//xs:attribute[@name='rows']"> 
    <jaxb:property name="rowCount"/> 
</jaxb:bindings> 

對於區分大小寫的問題,你可以使用XJC參數-XautoNameResolution,Maven的版本是<args><arg>-B-XautoNameResolution</arg></args>,但這不起作用對於包裝元素來說,所以對於這些你需要編寫如上所述的jaxb自定義設置......如果你不喜歡我:)你可能需要使用xsd的本地副本並手動修復重複。

編輯 發現大小寫敏感性問題的另一個解決方案,其中重命名元素名稱是不夠的:

<jaxb:bindings node=".//xs:element[@name='imageDatum'][@type='gml:ImageDatumPropertyType']"> 
    <jaxb:property name="imageDatumInst"/> 
    <jaxb:factoryMethod name="imageDatumInst" /> 
</jaxb:bindings> 

祝你好運,希望這有助於。

+7

-XautoNameResolution爲我解決了它:-D – 2016-04-25 09:03:40

2

我使用cxf從.wsdl生成對象時遇到同樣的問題。我的解決方法是使用-autoNameResolution作爲錯誤消息建議。 Maven配置:

  <wsdlOption> 
          <wsdl>${basedir}/test.wsdl</wsdl> 
          <extraargs> 
           <extraarg>-b</extraarg>  
          <extraarg>http://www.w3.org/2001/XMLSchema.xsd</extraarg> 
           <extraarg>-autoNameResolution</extraarg> 
          </extraargs> 
          <packagenames> 
           <packagename>test.wsdl</packagename> 
          </packagenames> 
         </wsdlOption> 
0

我試過以下,它的工作對我來說:

<jxb:bindings schemaLocation="ClaimActivity-ACORD.xsd"> 
    <jxb:schemaBindings> 
     <jxb:package name="x.x.x.x" /> 
    </jxb:schemaBindings> 
    <jxb:bindings node="//xsd:complexType[@name='ConstructionType']"> 
     <jxb:class name="AbstractConstructionType" /> 
    </jxb:bindings> 
    <jxb:bindings node="//xsd:complexType[@name='ItemDefinitionType']"> 
     <jxb:class name="AbstractItemDefinitionType" /> 
    </jxb:bindings> 
    <jxb:bindings node="//xsd:complexType[@name='LocationType']"> 
     <jxb:class name="AbstractLocationType" /> 
    </jxb:bindings> 
    <jxb:bindings node="//xsd:complexType[@name='TaxFeeType']"> 
     <jxb:class name="AbstractTaxFeeType" /> 
    </jxb:bindings> 
    <jxb:bindings node="//xsd:complexType[@name='DeductibleType']"> 
     <jxb:class name="AbstractDeductibleType" /> 
    </jxb:bindings> 
    <jxb:bindings node="//xsd:complexType[@name='RegistrationType']"> 
     <jxb:class name="AbstractRegistrationType" /> 
    </jxb:bindings> 
</jxb:bindings> 

1

另一種選擇是,除去-p選項,因此類在不同的包

產生