Generate XSD from Java Classes

In this tutorial we show you how to generate XSD from Java Classes using java binding annotations. Java Classes can be converted into XSD schema and vice versa using the jaxb2-maven-plugin. We can specify which classes need to be included and use a post processor to bind a specific namespace to a filename. This plugin requires that your classes are annotated with JAXB annotations.

Generate XSD from Java Classes with Maven

With the help of the jaxb2-maven-plugin we can generate XSD schemas from Java Classes. By default it scans all the folders in src/main/java recursively for annotated JAX-B Java Classes. But for this example we specify the source destination of our JAX-B annotated Classes. We also register a transformSchemas which is a post processor that is responsible for naming the XSD schema. It works by matching the namespace with the namespace of the @XmlType of your Java Class. Once we will build our project, it will generate XSD classes in target/generated-resources/schemagen directory.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""

    <name>XSD - ${project.artifactId}</name>




Java Class to be Transformed into XSD Schema

In order for a Java Class to be eligible for an XSD schema candidate, the class must be annotated with the @XmlType annotation. In this example we give the class a concrete namespace of this way we can uniquely identify the resource.

package com.memorynotfound;

import javax.xml.bind.annotation.*;
import java.util.Date;

@XmlType(name = "Course", propOrder = {
}, namespace = "")
public class Course {

    protected int id;
    @XmlElement(required = true)
    protected String name;
    @XmlElement(required = true)
    protected String description;
    @XmlElement(required = true)
    @XmlSchemaType(name = "dateTime")
    protected Date created;

    public int getId() {
        return id;

    public void setId(int id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getDescription() {
        return description;

    public void setDescription(String description) {
        this.description = description;

    public Date getCreated() {
        return created;

    public void setCreated(Date created) {
        this.created = created;

Generated XSD

Once the project is build, it will generate XSD classes in target/generated-resources/schemagen directory. This XSD Schema is the result of the previous Java Class.

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

    <xs:complexType name="Course">
            <xs:element name="id" type="xs:int"/>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="description" type="xs:string"/>
            <xs:element name="created" type="xs:dateTime"/>




You may also like...