Home » JAXB » Generate Java Class from xml Schema in Eclipse

Generate Java Class from xml Schema in Eclipse

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.


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0"
<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: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" />

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

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...

You can see the generated classes in Project explorer

Java Class from XSD

Download this example(Developed in Eclipse)

Previous Next Article

comments powered by Disqus