JAXB - 属性"值"已定义.使用<jaxb:property>解决此冲突

bra*_*zoo 59 java xsd jaxb

使用JAXB生成XML绑定类.

该架构基于一组遗留XML文件,并包含以下代码段:

<xs:complexType name="MetaType">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute type="xs:string" name="Name" />
            <xs:attribute type="xs:string" name="Scheme" />
            <xs:attribute type="xs:string" name="Value" />
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)

'Value'属性与'value'属性冲突,xs:string代码生成失败并出现错误:

com.sun.istack.SAXParseException2: Property "Value" is already defined. Use &lt;jaxb:property> to resolve this conflict. 
Run Code Online (Sandbox Code Playgroud)

bra*_*zoo 62

答案在于使用JAXB绑定(site-template.xjb):

<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
          xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          version="2.1">
    <bindings schemaLocation="site-template.xsd" version="1.0">
        <!-- Customise the package name -->
        <schemaBindings>
            <package name="com.example.schema"/>
        </schemaBindings>

        <!-- rename the value element -->
        <bindings node="//xs:complexType[@name='MetaType']">
            <bindings node=".//xs:attribute[@name='Value']">
                <property name="ValueAttribute"/>
            </bindings>
        </bindings>
    </bindings>
</bindings>
Run Code Online (Sandbox Code Playgroud)

XPath表达式定位节点并重命名它,从而避免命名冲突.

使用此绑定XML文件,生成的Java类最终具有所需的getValueAttribute()(以及getValue()).

  • 我应该在哪里放置这个`site-template.xjb`文件? (3认同)
  • 我也有这个问题,这个答案解决了,谢谢!想补充一点,如果使用Maven jaxb插件进行Java类生成,可以将xjb文件放在与实际XSD文件相同的资源目录中. (2认同)

Gar*_*ary 22

如果要避免创建/更改JAXB绑定文件,并且不介意注释XSD,可以将jxb:property注释添加到属性的定义中,例如:

<xs:complexType name="MetaType">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute type="xs:string" name="Name" />
            <xs:attribute type="xs:string" name="Scheme" />
            <xs:attribute type="xs:string" name="Value">
                <!-- rename property generated by JAXB (avoiding "Value" name conflict) -->
                <xs:annotation>
                    <xs:appinfo>
                        <jxb:property name="valueAttribute"/>
                    </xs:appinfo>
                </xs:annotation>
            </xs:attribute>
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)

添加了xs:schema标记:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
           jxb:version="2.1">
Run Code Online (Sandbox Code Playgroud)

  • 这对我来说好多了.接受的答案需要我无法管理的设置.我只是想让它起作用! (2认同)
  • 感谢它的帮助,-p xxx.xjb 更具挑战性。 (2认同)

小智 7

在为xxxx.xjb文件创建重复属性名称"value"(复制是JAXB提供的默认值'value)后,如下所示,运行XJC命令创建JAXB对象

xjc -p"com.track.doc"-d"C:\ JAXBDocuments\prasam\Desktop\JAXB_me\DealerTrace"appSamp.xsd -b xxxx.xjb

appSmp.xsd: -

<xsd:complexType name="range">
    <xsd:simpleContent>
        <xsd:extension base="xsd:string">
             <xsd:attribute name="value" type="xsd:string"/> 
        </xsd:extension>
    </xsd:simpleContent>        
</xsd:complexType>
Run Code Online (Sandbox Code Playgroud)

xxxx.xjb: -

<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
          xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          version="2.1">
    <bindings schemaLocation="appSmp.xsd" version="1.0">

        <schemaBindings>
            <package name="com.track.doc"/>
        </schemaBindings>    
        <bindings node="//xs:complexType[@name='range']">
            <bindings node=".//xs:attribute[@name='value']">
                <property name="valueAttribute"/>
            </bindings>
        </bindings>
    </bindings>
</bindings>
Run Code Online (Sandbox Code Playgroud)