【问题标题】:How to customize property name in JAXB?如何在 JAXB 中自定义属性名称?
【发布时间】:2015-01-18 07:25:29
【问题描述】:

我正在使用 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;
}

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

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

【问题讨论】:

  • 你可以随时自己编写类代码并使用@XmlElement 调整映射
  • @Kamil.H 显然不是 OP 所要求的。 :)
  • 能否请您也发布包含您的元素的复杂类型?

标签: java xml jaxb xsd


【解决方案1】:

您可以使用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>

(未测试。)

另见:

【讨论】:

  • 感谢您的回复。它工作正常,但这个解决方案意味着为 xsd 中的每个元素添加一个绑定。我遇到了这里描述的插件:stackoverflow.com/questions/8916398/… 这似乎完全符合我的需要。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-26
  • 1970-01-01
相关资源
最近更新 更多