How to create pojo classes from XSD?

My recommendation is to go with JAXB.

I have tested it in eclipse, works well for me. My suggestion is try generating the POJO from command line or with the help of eclipse. Once successful configure it with maven to generate the POJO build time.

There are several tutorials to learn this, please follow the below link(s) based upon your preference:

  • Generate POJO Class from XSD in Eclipse
  • Generate POJO class from XSD Schema command line
  • Generate POJO Classes from XSD using XJC Maven Plugin

Also the youtube links:

  • Youtube video tutorial
  • Youtube tutorial using maven

I hope it helps!

Feel free to comment if you encounter any issue.


jaxb2-maven-plugin

Using jaxb2-maven-plugin is the easiest way. Define the plugins as below :

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <schemaDirectory>${project.basedir}/src/main/xsd/</schemaDirectory>
                <schemaFiles>MARC21slim.xsd</schemaFiles>
            </configuration>
        </plugin>
    </plugins>
</build>

and execute :

mvn jaxb2:xjc

the generated files will be located in target\generated-sources\jaxb


jaxb2-maven-plugin version 2 changes how the configure.

The following will run xjc on everything in src/main/resource and put it com.yourcompany.xsd

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>2.5.0</version>
    <executions>
        <execution>
            <goals>
                <goal>xjc</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <sources>
            <source>src/main/resources</source>
        </sources>
        <packageName>com.yourcompany.xsd</packageName>
    </configuration>
</plugin>

Check out the implicit behavior in https://www.mojohaus.org/jaxb2-maven-plugin/Documentation/v2.5.0/example_xjc_basic.html


One simple way to convert .xsd files to Java file is xjc tool. Just execute the following command in the same working directory:

xjc test.xsd