【问题标题】:Generating Java classes out of XMLSchema.xsd using JAXB使用 JAXB 从 XMLSchema.xsd 生成 Java 类
【发布时间】:2019-02-08 00:26:43
【问题描述】:

我正在使用 jaxb 从 xml 模式中生成 java 类。 架构导入 XMLSchema.xsd,其内容用作文档中的元素。

如果我分别删除导入和对“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.

【问题讨论】:

    标签: java jaxb


    【解决方案1】:

    也与此类问题作斗争,对我来说,这是区分大小写的问题以及元素和属性问题的同名问题(有时通过继承)。对于第二个问题,我正在使用包含如下内容的外部绑定文件:

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

    对于区分大小写的问题,您可以使用 xjc 参数 -XautoNameResolution,maven 版本是 &lt;args&gt;&lt;arg&gt;-B-XautoNameResolution&lt;/arg&gt;&lt;/args&gt;,但这不适用于包装器元素,因此对于这些您需要将 jaxb 自定义编写为上面提到,...如果你像我一样不走运:) 你可能需要使用 xsd 的本地副本并手动修复重复。

    编辑 为重命名元素还不够的区分大小写问题找到了另一种解决方案:

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

    祝你好运,希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      您可以通过(至少)两种方法成功地让 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"。因此,在类名前加上“Abstract”是有意义的,即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 的答案描述了如何。

      【讨论】:

      • 面临类似问题。但是我有大约 50 个这样的变量有什么替代解决方案吗?
      【解决方案3】:

      另一个选项是删除-p 选项,以便在不同的包中生成类

      【讨论】:

      • 这就是我想要的。谢谢!
      • 是的,我试图通过指定一个包来保持整洁。一旦我把 -p 拿出来让它撕裂,它就没有问题了。谢谢克里斯托斯。
      【解决方案4】:

      您的架构似乎已损坏。 XJC非常对这些事情很挑剔,并且可能会在其他工具允许的事情上失败。

      我发现首先通过AlphaWorks Schema Quality Checker 运行架构更容易,而不是通过 XJC 的错误来挑选。这给出了一个很好的、人类可读的(好吧,无论如何,开发人员可读的)错误输出。如果它通过了,你将有更大的机会通过 XJC。

      【讨论】:

      • 感谢 SQC 的提示,尽管它无法识别上述问题。
      • 链接好像坏了。你能提供一个新的吗?
      【解决方案5】:

      我在使用 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>
      

      【讨论】:

        【解决方案6】:

        我遇到了同样的错误并完全删除了&lt;generatePackage&gt;&lt;/generatePackage&gt;。这解决了我的问题。

        【讨论】:

          【解决方案7】:

          我尝试了以下方法,它对我有用:

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

          【讨论】:

            【解决方案8】:

            你可以使用JDK1.6自带的xjc。你也可以尝试 XML to Java,参考this article

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-05-22
              • 2013-02-26
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多