Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
485 views
in Technique[技术] by (71.8m points)

java - How to customize property name in JAXB?

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

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

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

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().

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

(Not tested.)

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...