我正在使用 jaxb 从 xml 模式中生成 java 类。 架构导入 XMLSchema.xsd,其内容用作文档中的元素。
如果我分别删除导入和对“xsd:schema”的引用,那么绑定(bind)编译器会成功生成类。 如果我不这样做,则会产生以下错误,如果我尝试仅从 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.
最佳答案
也与此类问题作斗争,对我来说,这是区分大小写的问题以及元素和属性问题的同名问题(有时通过继承)。对于第二个问题,我正在使用包含如下内容的外部绑定(bind)文件:
<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>
祝你好运,希望这会有所帮助。
关于java - 使用 JAXB 从 XMLSchema.xsd 生成 Java 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2409278/