How to generate XSD from Java Class

In last few posts, we learned about Java JAXB and how to generate java class from XSD. Today we will learn how to generate XSD from java classes.

We will use JAXB-2 Maven Plugin in a maven project to generate XSD from java classes.

  • JAXB2 Maven Plugin uses JAXB SchemaGenerator utility to generate XSD from java classes.
  • Java classes should have JAXB annotations to be used by this plugin.
  • Minimum java version required is Java 5

First create a new maven project, you can give any name, group id and artifact id you want. Once we will build our project, it will generate XSD classes in target/generated-resources/schemagen directory.

After build, our project structure will look like below image.


Here is the final pom.xml file we have:

<project xmlns="" xmlns:xsi=""





Few things to notice are jaxb dependency, schemagen execution goal and transformSchema configuration. transformSchema configuration is used to specify the XSD file name generated and namespace prefix to be used in the XSD file.

Here are the java classes we have that will be used to generate XSD.

package com.journaldev.bean;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;

@XmlType(namespace = "")
public class Employee {
    private String name;
    private int id;
    private String role;
    private Address address;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public int getId() {
        return id;

    public void setId(int id) { = id;

    public String getRole() {
        return role;

    public void setRole(String role) {
        this.role = role;

    public Address getAddress() {
        return address;

    public void setAddress(Address address) {
        this.address = address;

Notice the XmlType annotation with namespace used for the class and XmlAttribute for the field id. This class will generate employee.xsd schema once we build the project. As you can see that it has a field Address that is another custom class, so we need to annotate this class also for successful schema generation.

Here is the address class with jaxb annotations.

package com.journaldev.bean;

import javax.xml.bind.annotation.XmlType;

@XmlType(namespace = "")
public class Address {
    private String city;
    private int zip;
    private String addressLine1;
    private String addressLine2;
    public String getCity() {
        return city;
    public void setCity(String city) { = city;
    public int getZip() {
        return zip;
    public void setZip(int zip) { = zip;
    public String getAddressLine1() {
        return addressLine1;
    public void setAddressLine1(String addressLine1) {
        this.addressLine1 = addressLine1;
    public String getAddressLine2() {
        return addressLine2;
    public void setAddressLine2(String addressLine2) {
        this.addressLine2 = addressLine2;

This class will generate address.xsd because it’s name is matched in transformSchema in pom.xml file.

Our project setup is ready, just build the project using command mvn clean install and the XSD files will be generated. For my project, generated XSD files are as below.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="" xmlns:addrns="" targetNamespace="" version="1.0">

  <xs:import namespace="" schemaLocation="address.xsd"/>

  <xs:complexType name="employee">
      <xs:element minOccurs="0" name="address" type="ns1:address"/>
      <xs:element minOccurs="0" name="name" type="xs:string"/>
      <xs:element minOccurs="0" name="role" type="xs:string"/>
    <xs:attribute name="id" type="xs:int" use="required"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="" targetNamespace="" version="1.0">

  <xs:complexType name="address">
      <xs:element minOccurs="0" name="addressLine1" type="xs:string"/>
      <xs:element minOccurs="0" name="addressLine2" type="xs:string"/>
      <xs:element minOccurs="0" name="city" type="xs:string"/>
      <xs:element name="zip" type="xs:int"/>

I hope you will find it useful and easy to understand.


Leave a Reply

Your email address will not be published. For posting CODE wrap them inside <pre class="prettyprint"></pre>. Required fields are marked *