Search code examples
javaxmljaxbjaxb2maven-jaxb2-plugin

Jaxb: How to specify a default class for an XSD element


When generating Java classes from a XSD, how can I specify that for some specific node, a specific and already existent Java class should be used instead of trying to generate one?

Thank you very much.


Solution

  • You can use episode file to reference the existing classes. .episode files are just jaxb bindings file and has mappings between elements and java classes.

    a) if those existing classes are also generated from (another) xsd. use below option to first create .episode file.

    xjc -episode a.episode a.xsd
    

    then use this a.episode that contains the mappings as input to the next xjc generation.

    xjc b.xsd -extension -b a.episode
    

    b) If you want to refer some random classes, then you may have to write your own episode file providing mapping between element and class reference like below.

    sample.episode

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" if-exists="true" version="2.1">
     <jaxb:bindings scd="x-schema::">
        <jaxb:bindings scd="employee">
          <jaxb:class ref="www1.example.Employee"/>
          <jaxb:package name="www1.example" />
        </jaxb:bindings>
       </jaxb:bindings>
    

    and use xjc b.xsd -extension -b sample.episode