How to generate java class from xml schema xsd using jaxb in eclipse

Let us discuss how to generate or create java class from xml schema xsd using jaxb in eclipse with below example.


1. Create a new JAXB project. File -> New -> Other -> JAXB -> JAXB Project.

2. Enter the project name and click on Finish button.

3. Download JAXB Jar and JAXB-XJC jar files and include in class path.

4. Create package which will contain xsd file.

5. Create package which will contain java classes.

6. Create xsd file.

7. Copy the below code into xsd file.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="">
<xs:element name="student" type="student"/>
<xs:element name="address" type="address"/>
<xs:complexType name="address">
<xs:element name="addressLine1" type="xs:string" minOccurs="0"/>
<xs:element name="addressLine2" type="xs:string" minOccurs="0"/>
<xs:element name="city" type="xs:string" minOccurs="0"/>
<xs:element name="state" type="xs:string" minOccurs="0"/>
<xs:complexType name="student">
<xs:element name="firstName" type="xs:string" minOccurs="0"/>
<xs:element name="lastName" type="xs:string" minOccurs="0"/>
<xs:element name="className" type="xs:string" minOccurs="0"/>
<xs:element name="rollNo" type="xs:string" minOccurs="0"/>
<xs:element ref="address" minOccurs="0"/>
8. Right click on your package -> New -> Other -> JAXB -> JAXB Classes from Schema.

9. Specify project for new Java classes and click on Next.

10. Select schema from which java classes have to generated and click on Next.

11. Specify package for new Java classes and click on Finish.


On Console:

parsing a schema...
compiling a schema...

No comments: