xsdjaxbdtdxjc

Up-to-date Documentation on JAXB Bindings for XSD and DTD


I'm trying to generate classes from XSD and DTD files while adding some customizations (have the generated classes implement an interface).

I found this thread about XSD bindings, however, it no longer functions.

Let's say I use a dummy bindings.xjb:

<jxb:bindings version="1.0" 
  xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
  jxb:extensionBindingPrefixes="xjc">
</jxb:bindings>

Calling xjc with:

xjc -b bindings.xjb example.xsd

XJC will complaint that the namespace http://java.sun.com/xml/ns/jaxb is not supported.

Looking at Oracle's site, I can only find Customizing JAXB Bindings which didn't say much about the namespaces other than:

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

which is also not working.

For DTD instead of XSD, This other thread mentioned that when using DTD, the bindings file is completely different and should look like this:

<?xml version="1.0"?>
    <xml-java-binding-schema version="1.0ea2">
       <element name="us-patent-grant" type="class" root="true"></element>
    </xml-java-binding-schema>

Searching with the root element's name, I cannot find documentation on its syntax.

Where are the documentation for the binding file used in xjc -b [file] option for both XSD and DTD?

EDIT: I think I've found the documentation for XSD here: https://javaee.github.io/jaxb-v2/doc/user-guide/ch03.html

But in my case, I only have a DTD, and that page didn't cover xml-java-binding-schema.


Solution

  • I think I've found it: http://xml.coverpages.org/jaxb0530spec.pdf

    The PDF is linked at the bottom of this page from Oracle: https://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.6/jaxb/vendorSchemaLangs.html#dtd

    For XSD it is: https://javaee.github.io/jaxb-v2/doc/user-guide/ch03.html

    Edit: Turns out there are more. First there are multiple JAXB maven plugins implemented by different parties. Not all of them support plugins and they expect different things in the binding XJB file.

    I ended up using this one, which has plugins for adding an interface:

    <groupId>org.jvnet.jaxb2.maven2</groupId>
                    <artifactId>maven-jaxb2-plugin</artifactId>
                    <version>0.15.3</version>
    

    The configuration is:

        <plugin>
            <!-- There are multiple JAXB plugins... but only this one supports the use of extensions -->
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.15.3</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <extension>true</extension>
                <args>
                    <arg>-Xinheritance</arg>
                </args>
                                <schemas>
                    <schema>
                        <fileset>
                            <directory>${project.basedir}/src/main/resources/schema</directory>
                            <includes>
                                <include>workflow_2_8.xsd</include>
                            </includes>
                        </fileset>
                    </schema>
                </schemas>
                <bindings>
                    <binding>
                        <fileset>
                            <directory>${project.basedir}/src/main/resources/schema</directory>
                            <includes>
                                <include>xsd_bindings.xjb</include>
                            </includes>
                        </fileset>
                    </binding>
                </bindings>
                <generateDirectory>${project.build.directory}/generated-sources/jaxb</generateDirectory>
                <plugins>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics</artifactId>
                        <version>0.6.4</version>
                    </plugin>
                </plugins>
            </configuration>
        </plugin>
    

    And the bindings I use:

    <?xml version="1.0"?>
    <jxb:bindings version="1.0"
        xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
        xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
        jxb:extensionBindingPrefixes="xjc">
        
        <jxb:bindings schemaLocation="workflow_2_8.xsd">
        
            <!-- Set output package -->
            <jxb:schemaBindings>
                <jxb:package name="output package name here"/>
            </jxb:schemaBindings>
    
            <!-- Make generated classes implement interface -->
            <jxb:bindings multiple="true" node="//xs:element/xs:complexType">
                <inheritance:implements>Interface name here
                </inheritance:implements>
            </jxb:bindings>
        
        </jxb:bindings>
        
    </jxb:bindings>
    

    Also need yet another plugin to add the generated source files to source path:

    <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                        <execution>
                            <id>add-source</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>add-source</goal>
                            </goals>
                            <configuration>
                                <sources>
                                    <source>${project.build.directory}/generated-sources/jaxb</source>
                                </sources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>