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:sequence>
<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:sequence>
<xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>


  <xsd:complexType name="USAddress">
<xsd:sequence>
<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:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/>
</xsd:complexType>
  <xsd:complexType name="Items">
<xsd:sequence>
<xsd:element name="item" minOccurs="1" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="productName" type="xsd:string"/>
<xsd:element name="quantity">
<xsd:simpleType>
<xsd:restriction base="xsd:positiveInteger">
<xsd:maxExclusive value="100"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="USPrice" type="xsd:decimal"/>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
</xsd:sequence>
<xsd:attribute name="partNum" type="SKU" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>


<!-- 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}"/>
</xsd:restriction>
</xsd:simpleType>

</xsd:schema>


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...
com\vaani\generated\Items.java
com\vaani\generated\ObjectFactory.java
com\vaani\generated\PurchaseOrderType.java
com\vaani\generated\USAddress.java

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;


@XmlAccessorType(AccessType.FIELD)
@XmlType(name = "USAddress", propOrder = {
"name",
"street",
"city",
"state",
"zip"
})
public class USAddress {

protected String name;
protected String street;
protected String city;
protected String state;
protected BigDecimal zip;
@XmlAttribute
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
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

Chitika