JaxB adds undescore(_) when generating Enumerations from XSD
enums, jaxb, maven-jaxb2-plugin, xml, xsd
Solution
You can use an external bindings file to customize the enum generation:
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jxb:bindings schemaLocation="schema.xsd">
<jxb:bindings node="//xs:simpleType[@name='MyEnum']/xs:restriction/xs:enumeration[@value='My_Simple_Text']">
<jxb:typesafeEnumMember name="FOO"/>
</jxb:bindings>
<jxb:bindings node="//xs:simpleType[@name='MyEnum']/xs:restriction/xs:enumeration[@value='MySimple_Text']">
<jxb:typesafeEnumMember name="BAR"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
UPDATE
Turns out the better approach is to use the following bindings file instead:
<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jxb:globalBindings underscoreBinding="asCharInWord"/>
</jxb:bindings>
Problem
I use maven JaxB plugin to generate source from XSD. The plugin details are as below, ``` <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <version>0.8.3</version> ``` Enumerations defined in XSD has two values, ``` <simpleType name="MyEnum"> <restriction base="xsd:string"> <enumeration value="SimpleText" /> <enumeration value="ComplexText" /> </restriction> </simpleType> ``` The generated code adds underscore between work boundaries of the enum values. For eg:, "SimpleText" comes as SIMPLE_TEXT in enum. Generated code, ``` @XmlType(name = "MyEnum") @XmlEnum public enum MyEnum { @XmlEnumValue("SimpleText") SIMPLE_TEXT("SimpleText"), @XmlEnumValue("ComplexText") COMPLEX_TEXT("ComplexText"); private final String value; MyEnum(String v) { value = v; } public String value() { return value; } public static MyEnum fromValue(String v) { for (MyEnum c: MyEnum.values()) { if (c.value.equals(v)) { return c; } } throw new IllegalArgumentException(v); } ``` } The problem occurs when an enumeration has same text separated with underscore at different places. For Eg; ``` <simpleType name="MyEnum"> <restriction base="xsd:string"> <enumeration value="My_Simple_Text" /> <enumeration value="MySimple_Text" /> </restriction> </simpleType> ``` doesn't convert to an enum. Is there any way to avoid JaxB adding underscore between words.