Getting started with jaxb – Code generation from XSD

JAXB can be used to generate classes from an model defined in XSD. It will then be possible to read XML document made against this XSD directly as java instances and inversly save these instances as XML document.

Take the following XSD saved in a file named SimpleModel.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns=""
 <complexType name="Person">
         <element name="FirstName" type="string"/>
         <element name="LastName" type="string"/>
         <element name="DateOfBirth" type="dateTime"/>

You can use JAXB to generate classes automatically to match this XSD using this command line (provided your JDK’s bin folder is on your path)

xjc SimpleModel.xsd

This will generate a package based on the namespace of your XSD (here mycorp.schemas.simplemodel) with the following classes :


The ObjectFactory is used to create instances of the class(es) that were generated. In some cases this seems like a trivial wrapper around a

new Person();

But in more complex cases it will create the proper wrappers around your instances providing the missing link to properly marshall and unmarshall the objects to and from XML.

The contains information about the XSD in general.

All other files are classes derived from the model described in the XSD. Here we only have since there is only one object in our model.

Using other command line arguments Jaxb and XJC will give you immense power over the generated code. XJC also provide means to use or create plugins to go beyoond and do things like :

  • Have the generated code implement an interface or extend a class.
  • Automatically generate toString, hashcode, equals etc with the class.
  • Automatically map between xml types (simple or complex) and JavaType.
  • Inject custom code or annotations in the generated code.

And much much more

You can also use other tools to interact with xjc for you, Maven plugins (at least 4 that I know of), Ant task etc. Often times these tools can perform things that would be difficult to get with just the Reference implementation thorugh command line.

if you want to reproduce, please indicate the source:
Getting started with jaxb – Code generation from XSD - CodeDay