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="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.memorynotfound.xml</groupId>
    <artifactId>generate-xsd-from-java</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>XSD - ${project.artifactId}</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <id>schemagen</id>
                        <goals>
                            <goal>schemagen</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <sources>
                        <source>src/main/java/com/memorynotfound</source>
                    </sources>
                    <transformSchemas>
                        <transformSchema>
                            <uri>http://memorynotfound.com/course</uri>
                            <toPrefix>c</toPrefix>
                            <toFile>course.xsd</toFile>
                        </transformSchema>
                    </transformSchemas>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

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 http://memorynotfound.com/course this way we can uniquely identify the resource.

package com.memorynotfound;

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

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Course", propOrder = {
        "id",
        "name",
        "description",
        "created"
}, namespace = "http://memorynotfound.com/course")
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) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.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="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://memorynotfound.com/course" version="1.0">

    <xs:complexType name="Course">
        <xs:sequence>
            <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"/>
        </xs:sequence>
    </xs:complexType>

</xs:schema>

References

Download

You may also like...