JAXB-属性“值”已定义。用 解决这个冲突

浏览:45日期:2024-02-20
如何解决JAXB-属性“值”已定义。用 解决这个冲突?

答案在于利用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>

XPath表达式查找节点并重命名节点,从而避免了命名冲突。

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

解决方法

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

‘Value’属性与的’value’属性冲突xs:string,并且代码生成失败并显示以下错误:

com.sun.istack.SAXParseException2: Property 'Value' is already defined. Use &lt;jaxb:property> to resolve this conflict.

相关文章: