如何在JAXB中自定义属性名称? [英] How to customize property name in JAXB?

查看:672
本文介绍了如何在JAXB中自定义属性名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using JAXB to generate java classes based on some XSD schemas. For an element such as:

< xsd:element name =REC_LOCtype =xsd:stringminOccurs =1/ >

jaxb生成以下代码:

jaxb generates the following code:

@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方法的命名约定。例如,对于字段REC_LOC,他们期望使用名为getRecLoc(String value)和setRecLoc()的方法,而不是getRECLOC()。

The problem is that we need to use some proprietary XML tools that rely on the naming convention of the getter/setter methods. For example, for the field REC_LOC they expect methods called getRecLoc(String value) and setRecLoc(), instead of getRECLOC().

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

Is there any way to customise the method names generated by jaxb?

推荐答案

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

You can use the jaxb:property customization to customize the property name.

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

(未经测试。)

参见还:

  • globalBindings/@enableJavaNamingConventions
  • nameXmlTransform

这篇关于如何在JAXB中自定义属性名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆