Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize property name in JAXB?

Tags:

java

xml

jaxb

xsd

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?

like image 505
alampada Avatar asked Oct 20 '22 22:10

alampada


1 Answers

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:

  • globalBindings/@enableJavaNamingConventions
  • nameXmlTransform
like image 122
lexicore Avatar answered Oct 29 '22 02:10

lexicore