Tuesday, March 22, 2011

JAXB : Generating java classes from xsd

Lets first get the xsd file from which we want to generate a java class:
1. Get the following xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
<xsd:element name="comment" type="xsd:string"/>
  <xsd:complexType name="PurchaseOrderType">
<xsd:element name="shipTo" type="USAddress"/>
<xsd:element name="billTo" type="USAddress"/>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="items" type="Items"/>
<xsd:attribute name="orderDate" type="xsd:date"/>

  <xsd:complexType name="USAddress">
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="street" type="xsd:string"/>
<xsd:element name="city" type="xsd:string"/>
<xsd:element name="state" type="xsd:string"/>
<xsd:element name="zip" type="xsd:decimal"/>
<xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/>
  <xsd:complexType name="Items">
<xsd:element name="item" minOccurs="1" maxOccurs="unbounded">
<xsd:element name="productName" type="xsd:string"/>
<xsd:element name="quantity">
<xsd:restriction base="xsd:positiveInteger">
<xsd:maxExclusive value="100"/>
<xsd:element name="USPrice" type="xsd:decimal"/>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
<xsd:attribute name="partNum" type="SKU" use="required"/>

<!-- Stock Keeping Unit, a code for identifying products -->
  <xsd:simpleType name="SKU">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\d{3}-[A-Z]{2}"/>


2. Open the command prompt and type xjc … if you get lots of messages that means you have xjc in it, otherwise you have to set it.

3. Generate the java class:

Let the xsd name above is bidding.xsd…Now in the command prompt type:

xjc -p com.vaani.generated -d src bidding.xsd

So xjc is our compiler, -p = package name…so our package name is com.vaani.generated
-d = src is the generated codes outputing directory name.

So the output is :

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

A factory class (ObjectFactory.java), consisting of methods to create instance objects, also gets generated.


Taking a look at the generated classes :

package com.vaani.generated;
import java.math.BigDecimal;
import javax.xml.bind.annotation.AccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import primer.po.USAddress;

@XmlType(name = "USAddress", propOrder = {
public class USAddress {

protected String name;
protected String street;
protected String city;
protected String state;
protected BigDecimal zip;
protected String country;

public String getName() {
return name;

public void setName(String value) {
this.name = value;

public String getStreet() {
return street;

public void setStreet(String value) {
this.street = value;

public String getCity() {
return city;

public void setCity(String value) {
this.city = value;

public String getState() {
return state;

public void setState(String value) {
this.state = value;

public BigDecimal getZip() {
return zip;

public void setZip(BigDecimal value) {
this.zip = value;

public String getCountry() {
if (country == null) {
return "US";
} else {
return country;

public void setCountry(String value) {
this.country = value;


See the eclipse project based on this.

No comments:

Post a Comment