Generate XML Schema from Java class using ‘schemagen’ tool

Environment Used:

  • JDK 6 (Java SE 6) or later.
  • Any notepad or IDE for writing the Java code.
  • JAXB 2.1 API

JAXB Introduction

Java Architecture for XML Binding (JAXB) allows Java developers to map Java classes to XML representations. JAXB provides two main features: the ability to marshal Java objects into XML and the inverse, i.e. to unmarshal XML back into Java objects.

JAXB provides “xjc” tool to convert XML Schema to class representations. In addition, JAXB includes a “schemagen” tool which can essentially perform the inverse of “xjc”, creating an XML Schema from a set of annotated classes.

In this example, we will see how to generate XML schema from Java class using schemagen tool from command line.

What is an XML schema?

XML schema describes the structure of the data elements and their relationships in an XML document. XML schema can be written using Document Type Definition (DTD), XML Schema Language, and RELAX NG. Previous versions of JAXB reference implementation worked with only DTD and the latest versions uses XML Schema Language. The XML Schema language can also be referred as XML Schema Definition (XSD). We can create many XML document from a XML schema. For easy understanding we can say that,
XML schema ⇔ Java class.
With one Java class, we can create many objects similarly with one XML schema we can create many XML documents.

We use the Java Architecture for XML Binding (JAXB) schema generator tool, schemagen, to generate a XML schema from Java source files or class files.

Process JDK Tool
From Java source file -> XML schema schemagen

‘schemagen’ Command Line Options

Usage:

schemagen [-options …]

Options:

-d Specify where to place processor and javac generated class files
-cp Specify where to find user specified files
-classpath Specify where to find user specified files
-help Display this usage message
-episode Specify to generate an episode file for separate compilation.
-version Prints the version information. If you specify this option, only the version information is output and typical command processing does not occur.

We are going to generate a XML schema for a XML document shown below.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee id="1">
    <name>Kumar</name>
    <salary>30000.0</salary>
    <designation>Junior Developer</designation>
    <address>
        <city>Chennai</city>
        <line1>242 Main Street</line1>
        <state>TN</state>
        <zipcode>600001</zipcode>
    </address>
</employee>

Create required Java files

We create two Java classes Employee and Address with proper JAXB annotations set as follows.

Employee.java

package com.theopentutorials.jaxb.beans;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "employee")
public class Employee {
	@XmlAttribute
	private int id;
	
	private String name;
	private double salary;
	private String designation;
	
	private Address address;

	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 double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	public String getDesignation() {
		return designation;
	}

	public void setDesignation(String designation) {
		this.designation = designation;
	}

	public Address getAddress() {
		return address;
	}

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

Address.java


package com.theopentutorials.jaxb.beans;

public class Address {
	private String line1;
	private String line2;
	private String city;
	private String state;
	private long zipcode;
	public String getLine1() {
		return line1;
	}
	public void setLine1(String line1) {
		this.line1 = line1;
	}
	public String getLine2() {
		return line2;
	}
	public void setLine2(String line2) {
		this.line2 = line2;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public long getZipcode() {
		return zipcode;
	}
	public void setZipcode(long zipcode) {
		this.zipcode = zipcode;
	}	
}

‘schemagen’ command for schema genearation

In Windows open Command Prompt (Windows button + R and type cmd) or Terminal in Linux and go to the folder (use cd command) where your java code exists.

  • Compile the above Java classes and place the classes in ‘bin’ folder. Type the following command,

    C:\Users\iByteCode\Desktop\JAXBCodes\JAXBSchemaGen>javac -d bin src\com\theopentutorials\jaxb\beans\*.java

  • Now type the following command to generate the schema from Java source files.

    C:\Users\iByteCode\Desktop\JAXBCodes\JAXBSchemaGen>schemagen -cp bin src\com\theopentutorials\jaxb\beans\Employee.java

Output

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.