4

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

Community
  • 1
  • 1
KumarRaja
  • 121
  • 4
  • 9
  • Any advise on this quesion. If MAVEN-JAXB2 plugin is not an answer, are there any other alternatives. – KumarRaja Jan 19 '15 at 15:09
  • You can customize the binding with xjb file. While generating the java artefacts from xsd use the xjb binding file and it will generate the java classes as per your cutomization in your xjb file. – Manmay Mar 18 '15 at 12:57

1 Answers1

5

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.

Manmay
  • 835
  • 7
  • 11