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

Download this example.
Next Topic: How to generate xml schema xsd from java class using jaxb in eclipse.
Previous Topic: JAXB unmarshalling – convert xml into java object example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.