Generate JAXB classes from xml schema

JAXB  is  widely using in processing XML documents.It is very easy ,if we  are able to generate JAXB class from XML schema. This chapter gives a tutorial about how to generate jaxb classes from xml schema.

Generate JAXB classes from xml schema

Here we are using eclipse indigo as IDE.Also Java 1.6 is using as Java environment.JDK can be downloaded from here.

The steps in generating JAXB class from XML schema are listed below.

Step1.Create a Java project  File-> New->Java project

Give the project name as XMLSample.

Step2.Create a folder with name ‘schema’ in XMLSample.(By right click on XMLSample —>New –>Folder)

Step3.Right click on folder  schema  –>New –>XML Schema File

Give schema name as student.xsd.Press Finish .

Step 4. Open the student.xsd in source mode.Edit the xsd file with the lines shown below.











Step5.Right click on the student.xsd file.Then New->JAXB class from XmL schema.Select project , package and everything.Press Finish at the end.This step creates the JAXB classes from the specified XSD file.

(If error is logged then , that means we need to download  more plugins . We can download those required plugins  from here. Download the jaxbw-plugin.zip .Extract the zip file and include the  JAR files to our application class path )

In our case Employee.java class will be created along with ObjectFactory.java.

The Employee.java is shown here.

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

/**
*

Java class for employee complex type.
*
*

The following schema fragment specifies the expected content contained within this class.
*
*

 * 
 *   
 *     
 *       
 *         
 *         
 *         
 *       
 *     
 *   
 * 
 * 

*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "employee", propOrder = {
"name",
"id",
"batch"
})
public class Employee {

@XmlElement(required = true)
protected String name;
protected int id;
@XmlElement(required = true)
protected String batch;

/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}

/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}

/**
* Gets the value of the id property.
*
*/
public int getId() {
return id;
}

/**
* Sets the value of the id property.
*
*/
public void setId(int value) {
this.id = value;
}

/**
* Gets the value of the batch property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getBatch() {
return batch;
}

/**
* Sets the value of the batch property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setBatch(String value) {
this.batch = value;
}

}

The generated JAXB class is using  to process XML documents.

See related posts:

XML  Processing in Java

DOM interface

SAX interface

StAX interface

JAXB