如何在 JAXB 中自定义属性名称? [英] How to customize property name in JAXB?
问题描述
我使用 JAXB 生成基于一些 XSD 模式的 java 类.对于一个元素,例如:
I am using JAXB to generate java classes based on some XSD schemas. For an element such as:
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;
}
问题是我们需要使用一些专有的 XML 工具,这些工具依赖于 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>
(未测试.)
另见:
这篇关于如何在 JAXB 中自定义属性名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!