有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从XMLSchema生成Java类。使用JAXB的xsd

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

如果我分别删除对“xsd:schema”的导入和引用,那么绑定编译器将成功生成这些类。 如果我不这样做,那么它将产生以下错误,如果我尝试从XMLSchema生成Java类,这些错误是相同的。仅xsd

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

共 (6) 个答案

  1. # 1 楼答案

    我遇到了同样的错误,并完全删除了<generatePackage></generatePackage>。这解决了我的问题

  2. # 2 楼答案

    看起来你的模式被破坏了。XJC对这些事情非常挑剔,在其他工具允许的事情上可能会失败

    我发现先通过AlphaWorks Schema Quality Checker运行模式比通过XJC的错误更容易。这提供了一个很好的、人类可读的(不管怎样,开发人员可读的)错误输出。如果它通过了,那么你也将有更好的机会通过XJC

  3. # 3 楼答案

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

  4. # 4 楼答案

    在从中生成对象时,我也遇到了同样的问题。使用cxf的wsdl。我的解决方案是使用错误消息建议的-autonamesolution。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>
    
  5. # 5 楼答案

    对我来说,这是区分大小写的问题,元素和属性的名称相同(有时通过继承)。对于第二个问题,我使用的外部绑定文件包含以下内容:

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

    祝你好运,希望这有帮助

  6. # 6 楼答案

    您可以通过(至少)两种方法成功地让JAXB为XML模式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的回答描述了如何进行