如何在JAXB中自定义属性名称?

时间:2014-11-20 12:21:31

标签: java xml jaxb xsd

我正在使用JAXB基于某些XSD架构生成java类。对于如下元素:

<xsd:element name="REC_LOC" type="xsd:string" minOccurs="1"/>

jaxb生成以下代码:

@XmlElement(name = "REC_LOC", required = true)
protected String recloc;

public String getRECLOC() {
    return recloc;
}

/**
 * Sets the value of the recloc property.
 * 
 * @param value
 *     allowed object is
 *     {@link String }
 *     
 */
public void setRECLOC(String value) {
    this.recloc = value;
}

问题是我们需要使用一些依赖于getter / setter方法的命名约定的专有XML工具。例如,对于字段REC_LOC,它们期望使用名为getRecLoc(String value)和setRecLoc()的方法,而不是getRECLOC()。

有没有办法自定义jaxb生成的方法名?

1 个答案:

答案 0 :(得分:3)

您可以使用jaxb:property自定义来自定义属性名称。

<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="schema.xsd" version="1.0" node="/xs:schema">
        <bindings node="xs:complexType[@name='SOME_TYPE']">
            <bindings node="xs:sequence/xs:element[@name='REC_LOC']">
                <property name="RecLoc"/>
            </bindings>
        </bindings>
    </bindings>
</bindings>

(未经测试。)

另见:

相关问题