How to disable Java Naming Conventions in xjc?

Smertokogt picture Smertokogt · Jun 7, 2011 · Viewed 11.3k times · Source

For example sOmE_PROPerty in xsd must be sOmE_PROPerty in java class not someProperty.

I tried to use globalBindings enableJavaNamingConventions="false" but it doesn't work.

Answer

bdoughan picture bdoughan · Jun 7, 2011

You will need to use underscoreBinding="asCharInWord" instead of enableJavaNamingConventions="false":

customer.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema 
    targetNamespace="http://www.example.org/customer" 
    xmlns="http://www.example.org/customer"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"> 

    <xsd:complexType name="customer">
                <xsd:sequence>
                    <xsd:element name="sOmE_PROPerty" type="xsd:string"/>
                </xsd:sequence>
    </xsd:complexType>

</xsd:schema>

binding.xml

A JAXB binding file is used to customize the schema to Java conversion:

<jaxb:bindings 
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">
    <jaxb:globalBindings underscoreBinding="asCharInWord"/>
</jaxb:bindings>

XJC Call

xjc -d out -b binding.xml customer.xsd

Customer

The generated property names now include the underscore character:

package org.example.customer;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "customer", propOrder = {
    "sOmEPROPerty"
})
public class Customer {

    @XmlElement(name = "sOmE_PROPerty", required = true)
    protected String sOmEPROPerty;

    public String getSOmE_PROPerty() {
        return sOmEPROPerty;
    }

    public void setSOmE_PROPerty(String value) {
        this.sOmEPROPerty = value;
    }

}

Without Using binding.xml

If you instead make the following XJC call:

xjc -d out -customer.xsd

You will see that the generated properties do not include the underscore:

package org.example.customer;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "customer", propOrder = {
    "sOmEPROPerty"
})
public class Customer {

    @XmlElement(name = "sOmE_PROPerty", required = true)
    protected String sOmEPROPerty;

    public String getSOmEPROPerty() {
        return sOmEPROPerty;
    }

    public void setSOmEPROPerty(String value) {
        this.sOmEPROPerty = value;
    }

}