Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize the field name of the generated Java object using JAXB custom bindings?

I'm trying to generate JAXB generated objects for the below sample xsd.

<xs:complexType name="AddressType">   
      <xs:sequence>   
        <xs:element name="USA" type="xs:string"/>   
      </xs:sequence>   
   </xs:complexType>  

and the class that gets generated without any custom bindings is

@XmlAccessorType(XmlAccessType.FIELD)   
@XmlType(name = "AddressType", propOrder = {   
    "usa"  
})   
public class AddressType {   

    @XmlElement(name = "USA", required = true)   
    protected String usa;   

    /**  
     * Gets the value of the usa property.  
     *   
     * @return  
     *     possible object is  
     *     {@link String }  
     *       
     */  
    public String getUSA() {   
        return usa;   
    }   

    /**  
     * Sets the value of the usa property.  
     *   
     * @param value  
     *     allowed object is  
     *     {@link String }  
     *       
     */  
    public void setUSA(String value) {   
        this.usa = value;   
    }   
}  

as you see the field name is "usa" and setters/getters are getUSA/setUSA.

Is there any custom setting/binding to have the field name also be generated as "USA" instead of "usa", that way the field and property are all "USA".

I referred How to customize property name in JAXB?

but that is to customize the property, instead of field.. any help

By the way, I'm using maven-jaxb2-plugin

like image 746
KumarRaja Avatar asked Oct 25 '25 04:10

KumarRaja


1 Answers

Example xjb file as in your case : binding.xjb

example :

<jaxb:bindings 
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.1">

<jaxb:bindings schemaLocation="schema.xsd">
    <jaxb:bindings node="//xs:element[@name='USA']">
        <jaxb:property name="usa" />
    </jaxb:bindings>
</jaxb:bindings>

</jaxb:bindings>

Add -b binding.xjb with your xjc command or configure the binding file location in your maven xjc plugin.

like image 74
Manmay Avatar answered Oct 27 '25 01:10

Manmay