Generate XML Schema from Java class in Eclipse IDE

In the previous example provided here, we saw how to generate XML Schema from Java classes using ‘schemagen’ command line tool. Now we will see how to create the same in Eclipse IDE. We will use the same XML document as mentioned in the previous example.

Environment Used:

  • JDK 6 (Java SE 6) or later.
  • Eclipse Indigo IDE for Java EE Developers (3.7.1).
  • JAXB 2.1 API

Create JAXB Project

  1. Open Eclipse IDE and create a new JAXB project which can be done in many ways,
    • File menu -> New -> Other -> JAXB -> JAXB Project
    • Click on the down arrow on New icon on toolbar -> Other -> JAXB -> JAXB Project
    • Right click on Project Explorer -> New -> Other -> JAXB -> JAXB Project

    Select JAXB Project and click Next.

  2. Enter the project name as “SchemaGenJAXBPjt” and make sure jre6 is selected as the Target runtime with JAXB version 2.1.
  3. Click Next -> Next.

    NOTE: for JRE version 6, the JAXB version is 2.1. If you are using JRE7 then you can use JAXB 2.2.

  4. In JAXB Facet window, select “Generic JAXB 2.1” as platform with JRE as JAXB implementation as shown below and click Finish.

The first thing to remember about JAXB is that, it is a specification for which there are many implementations like JaxMeAPI, EclipseLink MOXy, and Metro. The JAXB shipped with JDK is a reference implementation. For simplicity, we are using this Generic reference implementation.

Create required Java files

Right click on ‘src’ and create a new package and name it as “com.theopentutorials.jaxb.beans”. We create two Java classes Employee and Address with proper JAXB annotations set as follows.

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;

@XmlRootElement(name = "employee")
public class Employee {
	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) { = id;

	public String getName() {
		return name;

	public void setName(String 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;

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) { = 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;

Generating schema from Java classes

  • Right click on your package -> New -> Other… -> JAXB -> Schema from JAXB Classes.
  • Click Next.
  • Specify file name and location.
  • Specify classes to include in schema generation and click Finish.


You will see the following message displayed in console.


generating schema…

Schema C:\Users\iByteCode\Desktop\JAXBCodes\SchemaGenJAXBPjt\src\com\theopentutorials\jaxb\beans\employeeSchema1.xsd generated

It generates the schema as shown below.

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

  <xs:element name="employee" type="employee"/>

  <xs:complexType name="address">
      <xs:element name="city" type="xs:string" minOccurs="0"/>
      <xs:element name="line1" type="xs:string" minOccurs="0"/>
      <xs:element name="line2" type="xs:string" minOccurs="0"/>
      <xs:element name="state" type="xs:string" minOccurs="0"/>
      <xs:element name="zipcode" type="xs:long"/>

  <xs:complexType name="employee">
      <xs:element name="name" type="xs:string" minOccurs="0"/>
      <xs:element name="salary" type="xs:double"/>
      <xs:element name="designation" type="xs:string" minOccurs="0"/>
      <xs:element name="address" type="address" minOccurs="0"/>
    <xs:attribute name="id" type="xs:int" use="required"/>

The complete folder structure for this example is shown below.

Leave a Comment