Generate Java Class from xml Schema in Eclipse

In this article we will discuss about how to generate java classes from XSD in Eclipse IDE.We are going to use MOXy JAXB implementation which is available as part of Eclipselink project.


Tools Used :

  1. JDK 6 (Java SE 6) or later.
  2. Eclipse Indigo IDE for Java EE Developers (3.7.1).
  3. EclipseLink 2.3.2 .

Download EclipseLink from here and extract the zip file in your local. We need this for MOXy which is an implementation of JAXB API.

In this article we will discuss about how to generate java classes from XSD in Eclipse IDE.We are going to use MOXy JAXB implementation which is available as part of Eclipselink project.


Tools Used :

  1. JDK 6 (Java SE 6) or later.
  2. Eclipse Indigo IDE for Java EE Developers (3.7.1).
  3. EclipseLink 2.3.2 .

Download EclipseLink from here and extract the zip file in your local. We need this for MOXy which is an implementation of JAXB API.






Steps to generate Java classes from XSD in Eclipse :

Follow the steps below to generate Java classes from XML Schema in Eclipse IDE.


Step 1: Create JAXB project

Open Eclipse IDE and create a new JAXB project which can be done by navigating to File-> New -> Other -> JAXB -> JAXB Project. Select JAXB Project and click Next.

Java Class from XSD


Step 2: Assign name of your Project

Enter the project name as JavaFromXsd and make sure jre7 is selected as the Target runtime and JAXB version as 2.2.If your project is using jre6 then you have to use JAXB version as 2.1. and Click Next -> Next.

Java Class from XSD


Step 3: JAXB Facet Setup

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

Java Class from XSD



Step 4: Create XSD file

Right click on 'src' and create a new package "com.example.jaxb.schema". Now create a new XSD file inside this package. To create a new XSD file, Right click on package -> New -> Other.-> XML -> XML Schema File and click Next.

Java Class from XSD


Select the location and enter the file name as 'employeerequest.xsd'. Copy the following code in XSD file.

employeerequest.xsd
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="employee" type="employee" />
<xs:complexType name="address"><xs:sequence>
<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:sequence>
</xs:complexType>
 
<xs:complexType name="employee">
<xs:sequence>
<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:sequence>
<xs:attribute name="id" type="xs:int" use="required" />
</xs:complexType>

<xs:complexType name="department">
<xs:sequence>
<xs:element name="name" type="xs:string" minOccurs="0" />
<xs:element name="address1" type="xs:string" minOccurs="0" />
<xs:element name="address2" type="xs:string" minOccurs="0" />
<xs:element name="phone" type="xs:string" minOccurs="0" />
<xs:element name="zipcode" type="xs:long" />
</xs:sequence>
</xs:complexType>
</xs:schema>

Step 5: Adding jar files to Project

We need to add MOXy JAXB implementation JAR files in the project classpath.Follow the steps below to do the same.
Right Click on your Project and select New-> folder provide the name of folder as lib and click Finish.
Now copy com.sun.tools.xjc.[version number].jar,com.sun.xml.bind.[version number].jar,javax.xml.bind.[version number].jar in lib folder.
You can found these jars in extracted [DOWNLOADED_ECLIPSELINK_FOLDER]/jlib/moxy folder.
Right click on your JAXB Project-> Build Path-> Configure Build Path and select Libraries tab from right side and click on Add External JARs.Add all the jars of lib folder.

Java Class from XSD


Step 6: Generating Java classes

Right click on your package -> New -> Other. -> JAXB -> JAXB Classes from Schema.

Java Class from XSD

Click Next and select Project (JavaFromXsd) and click Next.

Java Class from XSD

Select schema to generate classes from and click Next.

Java Class from XSD

Enter the package name "com.example.jaxb.generated" for the generated Java classes and Click Next-> Next-> Finish(dont change default values while navigating).

Java Class from XSD

After Finishing Click OK if you are getting any Warnings.You will see the following message displayed in the console.

parsing a schema...
compiling a schema...
com\example\jaxb\generated\Address.java
com\example\jaxb\generated\Department.java
com\example\jaxb\generated\Employee.java
com\example\jaxb\generated\ObjectFactory.java

You can see the generated classes in Project explorer

Java Class from XSD

Download this example(Developed in Eclipse)





comments powered by Disqus