JAXB: How to change XJC-generated classes names when attr type is specified in XSD?

JAXB provides two ways to accomplish this:

1. Inline Schema Anntotations

You can use JAXB schema annotations to control the class names.

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

    <xs:complexType name="itemType">
        <xs:annotation>
            <xs:appinfo>
                <jaxb:class name="Item"/>
            </xs:appinfo>
        </xs:annotation>
        <xs:attribute name="id" type="xs:string" use="required"/>
    </xs:complexType>

</xs:schema>

2. External Binding File

This customization can also be done via and external binding file:

<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="your-schema.xsd">
            <jxb:bindings node="//xs:complexType[@name="itemType"]">
                <jxb:class name="Item"/>
            </jxb:bindings>
    </jxb:bindings>

</jxb:bindings>

The xjc command line would be:

xjc -d out -b binding.xml your-schema.xsd

Leave a Comment