Saturday 20 August 2016

jaxb: Generate classes from XML Schema

In this post, I am going to show you how to generate Java classes from an xml schema. If you want to learn about XML schema, you can read from following post.

Following step-by-step procedures shows you how to generate Java classes from employee.xsd file.


employee.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

 <!-- Schema for simple elements -->
        <xs:element name="id" type="xs:integer" />
 <xs:element name="firstname" type="xs:string" />
 <xs:element name="middlename" type="xs:string" />
 <xs:element name="lastname" type="xs:string" />
        <xs:element name="salary" type="xs:decimal" />
        <xs:element name="married" type="xs:string" />
        <xs:element name="department" type="xs:string" />
        <xs:element name="pjtname" type="xs:string" />
 <xs:element name="from" type="xs:date" />
 <xs:element name="to" type="xs:date" />

        <xs:element name="city" type="xs:string" />
        <xs:element name="district" type="xs:string" />
        <xs:element name="state" type="xs:string" />
        <xs:element name="country" type="xs:string" />

        <!-- validate specialities -->
        <xs:element name="specialities">
            <xs:simpleType>
                <xs:list itemType="xs:string" />
            </xs:simpleType>
        </xs:element>

        <!-- validate satifiedWithWork -->
        <xs:element name="satifiedWithWork">
            <xs:simpleType>
                <xs:restriction base="xs:boolean">
                </xs:restriction>
            </xs:simpleType>
        </xs:element>

        <!-- validate taxPaid -->
        <xs:element name="taxPaid">
            <xs:simpleType>
                <xs:restriction base="xs:decimal">
                    <xs:fractionDigits value="2" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>

        <!-- validate mobileNum -->
        <xs:element name="empNum">
            <xs:simpleType>
                <xs:restriction base="xs:integer">
                    <xs:totalDigits value="9" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>

        <!-- validate descendents -->
        <xs:element name="descendants">
            <xs:simpleType>
                <xs:restriction base="xs:integer">
                    <xs:minInclusive value="1" />
                    <xs:maxInclusive value="7" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>

        <!-- validate taxEmption -->
        <xs:element name="taxExemption">
            <xs:simpleType>
                <xs:restriction base="xs:float">
                    <xs:maxExclusive value="250000.1" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>

        <!-- validate description -->
        <xs:element name="description">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:whiteSpace value="collapse" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>

        <!-- validate pin -->
        <xs:element name="pin">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:minLength value="5" />
                    <xs:maxLength value="8" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>

        <!-- validate sex -->
        <xs:element name="sex">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:enumeration value="male" />
                    <xs:enumeration value="female" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>

        <!-- Applying restriction on Employee age -->
        <xs:element name="age">
            <xs:simpleType>
                <xs:restriction base="xs:integer">
                    <xs:minInclusive value="19" />
                    <xs:maxExclusive value="56" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
        
 <!-- Schema for element details -->
 <xs:element name="details">
  <xs:complexType>
   <xs:sequence>
                            <xs:element ref="firstname" />
                            <xs:element ref="middlename" />
                            <xs:element ref="lastname" />
                            <xs:element ref="age" />
                            <xs:element ref="salary" />
                            <xs:element ref="married" />
                            <xs:element ref="sex" />
                            <xs:element ref="address" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>

        <!-- schema for address -->
        <xs:element name="address">
            <xs:complexType>
                <xs:sequence>
                    <xs:element ref="city" />
                    <xs:element ref="district" />
                    <xs:element ref="state" />
                    <xs:element ref="country" />
                    <xs:element ref="pin" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>

         <!-- Schema for element project -->
 <xs:element name="project">
  <xs:complexType>
   <xs:sequence>
                            <xs:element ref="department" />
                            <xs:element ref="pjtname" />
                            <xs:element ref="from" />
                            <xs:element ref="to" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>

         <!-- Schema for element employee -->
 <xs:element name="employee">
  <xs:complexType>
   <xs:sequence>
                            <xs:element ref="id" />
                            <xs:element ref="description" />
                            <xs:element ref="taxExemption" />
                            <xs:element ref="taxPaid" />
                            <xs:element ref="descendants" />
                            <xs:element ref="empNum" />
                            <xs:element ref="specialities" />
                            <xs:element ref="satifiedWithWork" />
                            <xs:element ref="details" />
                            <xs:element ref="project" maxOccurs="unbounded" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>

          <!-- Schema for element employees -->
 <xs:element name="employees">
  <xs:complexType>
   <xs:sequence>
                            <xs:element ref="employee" maxOccurs="unbounded" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 
</xs:schema>

Following employee.xml file confirms to employee.xsd file.


employee.xml
<?xml version="1.0" encoding="UTF-8"?>
   
<employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="employees.xsd">

    <employee>
        <id>1</id>
        <description> I am IT professional with 3Yrs of Experience </description>
        <taxExemption>240000</taxExemption>
        <taxPaid>89000.23</taxPaid>
        <descendants >3</descendants >
        <empNum>123456789</empNum>
        <specialities>C Java Threads XML Threads</specialities>
        <satifiedWithWork>true</satifiedWithWork>
        <details>
            <firstname>Hari</firstname>
            <middlename>Krishna</middlename>
            <lastname>Gurram</lastname>
            <age>25</age>
            <salary>80000</salary>
            <married>single</married>
            <sex>male</sex>
            <address>
                <city>Ongole</city>
                <district>prakasam</district>
                <state>Andhra Pradesh</state>
                <country>India</country>
                <pin>523169</pin>
            </address>
        </details>

        <project>
            <department>aero</department>
            <pjtname>Flight Controls</pjtname>
            <from>2011-09-16</from>
            <to>2013-01-22</to>
        </project>

        <project>
            <department>banking</department>
            <pjtname>Online Transaction Processing System</pjtname>
            <from>2013-01-23</from>
            <to>2014-10-06</to>
        </project>

        <project>
            <department>mobile</department>
            <pjtname>Daily News Application</pjtname>
            <from>2014-10-07</from>
            <to>2015-05-06</to>
        </project>
    </employee>
</employees>


Step 1: Create new jaxb project in eclipse. Open Eclipse. Go to File -> New -> Other.

Select JAXB project and press Next.

Give the project name as ‘jaxb_tutorial’. JAXB requires jdk (not jre), make sure you selected jdk.

Step 2: Copy the employees.xsd file into the project.
Step 2: Right click on the project, go to New -> Other -> JAXB. Select “JAXB classes from Schema”, press Next.

Select the destination folder for the generated classes. Press Next. Now select the xsd file. Press Finish. It generates following classes.

Address.java
Details.java
Employee.java
Employees.java
Project.java
ObjectFactory.java

Out of these classes ObjectFactory is different, it is used to marshal and unmarshal the data. Next posts, explain how to unmarshal and marshal the data using ObjectFactory class.

Address.java
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.2-147 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2016.08.19 at 10:11:35 PM IST 
//


package generated;

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


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element ref="{}city"/>
 *         &lt;element ref="{}district"/>
 *         &lt;element ref="{}state"/>
 *         &lt;element ref="{}country"/>
 *         &lt;element ref="{}pin"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "city",
    "district",
    "state",
    "country",
    "pin"
})
@XmlRootElement(name = "address")
public class Address {

    @XmlElement(required = true)
    protected String city;
    @XmlElement(required = true)
    protected String district;
    @XmlElement(required = true)
    protected String state;
    @XmlElement(required = true)
    protected String country;
    @XmlElement(required = true)
    protected String pin;

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

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

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

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

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

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

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

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

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

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

}


Details.java
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.2-147 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2016.08.19 at 10:11:35 PM IST 
//


package generated;

import java.math.BigDecimal;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element ref="{}firstname"/>
 *         &lt;element ref="{}middlename"/>
 *         &lt;element ref="{}lastname"/>
 *         &lt;element ref="{}age"/>
 *         &lt;element ref="{}salary"/>
 *         &lt;element ref="{}married"/>
 *         &lt;element ref="{}sex"/>
 *         &lt;element ref="{}address"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "firstname",
    "middlename",
    "lastname",
    "age",
    "salary",
    "married",
    "sex",
    "address"
})
@XmlRootElement(name = "details")
public class Details {

    @XmlElement(required = true)
    protected String firstname;
    @XmlElement(required = true)
    protected String middlename;
    @XmlElement(required = true)
    protected String lastname;
    protected int age;
    @XmlElement(required = true)
    protected BigDecimal salary;
    @XmlElement(required = true)
    protected String married;
    @XmlElement(required = true)
    protected String sex;
    @XmlElement(required = true)
    protected Address address;

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

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

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

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

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

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

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

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

    /**
     * Gets the value of the salary property.
     * 
     * @return
     *     possible object is
     *     {@link BigDecimal }
     *     
     */
    public BigDecimal getSalary() {
        return salary;
    }

    /**
     * Sets the value of the salary property.
     * 
     * @param value
     *     allowed object is
     *     {@link BigDecimal }
     *     
     */
    public void setSalary(BigDecimal value) {
        this.salary = value;
    }

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

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

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

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

    /**
     * Gets the value of the address property.
     * 
     * @return
     *     possible object is
     *     {@link Address }
     *     
     */
    public Address getAddress() {
        return address;
    }

    /**
     * Sets the value of the address property.
     * 
     * @param value
     *     allowed object is
     *     {@link Address }
     *     
     */
    public void setAddress(Address value) {
        this.address = value;
    }

}


Employee.java
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.2-147 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2016.08.19 at 10:11:35 PM IST 
//


package generated;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlList;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element ref="{}id"/>
 *         &lt;element ref="{}description"/>
 *         &lt;element ref="{}taxExemption"/>
 *         &lt;element ref="{}taxPaid"/>
 *         &lt;element ref="{}descendants"/>
 *         &lt;element ref="{}empNum"/>
 *         &lt;element ref="{}specialities"/>
 *         &lt;element ref="{}satifiedWithWork"/>
 *         &lt;element ref="{}details"/>
 *         &lt;element ref="{}project" maxOccurs="unbounded"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "id",
    "description",
    "taxExemption",
    "taxPaid",
    "descendants",
    "empNum",
    "specialities",
    "satifiedWithWork",
    "details",
    "project"
})
@XmlRootElement(name = "employee")
public class Employee {

    @XmlElement(required = true)
    protected BigInteger id;
    @XmlElement(required = true)
    protected String description;
    protected float taxExemption;
    @XmlElement(required = true)
    protected BigDecimal taxPaid;
    protected int descendants;
    @XmlElement(required = true)
    protected BigInteger empNum;
    @XmlList
    @XmlElement(required = true)
    protected List<String> specialities;
    protected boolean satifiedWithWork;
    @XmlElement(required = true)
    protected Details details;
    @XmlElement(required = true)
    protected List<Project> project;

    /**
     * Gets the value of the id property.
     * 
     * @return
     *     possible object is
     *     {@link BigInteger }
     *     
     */
    public BigInteger getId() {
        return id;
    }

    /**
     * Sets the value of the id property.
     * 
     * @param value
     *     allowed object is
     *     {@link BigInteger }
     *     
     */
    public void setId(BigInteger value) {
        this.id = value;
    }

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

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

    /**
     * Gets the value of the taxExemption property.
     * 
     */
    public float getTaxExemption() {
        return taxExemption;
    }

    /**
     * Sets the value of the taxExemption property.
     * 
     */
    public void setTaxExemption(float value) {
        this.taxExemption = value;
    }

    /**
     * Gets the value of the taxPaid property.
     * 
     * @return
     *     possible object is
     *     {@link BigDecimal }
     *     
     */
    public BigDecimal getTaxPaid() {
        return taxPaid;
    }

    /**
     * Sets the value of the taxPaid property.
     * 
     * @param value
     *     allowed object is
     *     {@link BigDecimal }
     *     
     */
    public void setTaxPaid(BigDecimal value) {
        this.taxPaid = value;
    }

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

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

    /**
     * Gets the value of the empNum property.
     * 
     * @return
     *     possible object is
     *     {@link BigInteger }
     *     
     */
    public BigInteger getEmpNum() {
        return empNum;
    }

    /**
     * Sets the value of the empNum property.
     * 
     * @param value
     *     allowed object is
     *     {@link BigInteger }
     *     
     */
    public void setEmpNum(BigInteger value) {
        this.empNum = value;
    }

    /**
     * Gets the value of the specialities property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the specialities property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getSpecialities().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link String }
     * 
     * 
     */
    public List<String> getSpecialities() {
        if (specialities == null) {
            specialities = new ArrayList<String>();
        }
        return this.specialities;
    }

    /**
     * Gets the value of the satifiedWithWork property.
     * 
     */
    public boolean isSatifiedWithWork() {
        return satifiedWithWork;
    }

    /**
     * Sets the value of the satifiedWithWork property.
     * 
     */
    public void setSatifiedWithWork(boolean value) {
        this.satifiedWithWork = value;
    }

    /**
     * Gets the value of the details property.
     * 
     * @return
     *     possible object is
     *     {@link Details }
     *     
     */
    public Details getDetails() {
        return details;
    }

    /**
     * Sets the value of the details property.
     * 
     * @param value
     *     allowed object is
     *     {@link Details }
     *     
     */
    public void setDetails(Details value) {
        this.details = value;
    }

    /**
     * Gets the value of the project property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the project property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getProject().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link Project }
     * 
     * 
     */
    public List<Project> getProject() {
        if (project == null) {
            project = new ArrayList<Project>();
        }
        return this.project;
    }

}


Project.java
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.2-147 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2016.08.19 at 10:11:35 PM IST 
//


package generated;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.datatype.XMLGregorianCalendar;


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element ref="{}department"/>
 *         &lt;element ref="{}pjtname"/>
 *         &lt;element ref="{}from"/>
 *         &lt;element ref="{}to"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "department",
    "pjtname",
    "from",
    "to"
})
@XmlRootElement(name = "project")
public class Project {

    @XmlElement(required = true)
    protected String department;
    @XmlElement(required = true)
    protected String pjtname;
    @XmlElement(required = true)
    @XmlSchemaType(name = "date")
    protected XMLGregorianCalendar from;
    @XmlElement(required = true)
    @XmlSchemaType(name = "date")
    protected XMLGregorianCalendar to;

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

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

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

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

    /**
     * Gets the value of the from property.
     * 
     * @return
     *     possible object is
     *     {@link XMLGregorianCalendar }
     *     
     */
    public XMLGregorianCalendar getFrom() {
        return from;
    }

    /**
     * Sets the value of the from property.
     * 
     * @param value
     *     allowed object is
     *     {@link XMLGregorianCalendar }
     *     
     */
    public void setFrom(XMLGregorianCalendar value) {
        this.from = value;
    }

    /**
     * Gets the value of the to property.
     * 
     * @return
     *     possible object is
     *     {@link XMLGregorianCalendar }
     *     
     */
    public XMLGregorianCalendar getTo() {
        return to;
    }

    /**
     * Sets the value of the to property.
     * 
     * @param value
     *     allowed object is
     *     {@link XMLGregorianCalendar }
     *     
     */
    public void setTo(XMLGregorianCalendar value) {
        this.to = value;
    }

}


Employees.java
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.2-147 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2016.08.19 at 10:11:35 PM IST 
//


package generated;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element ref="{}employee" maxOccurs="unbounded"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "employee"
})
@XmlRootElement(name = "employees")
public class Employees {

    @XmlElement(required = true)
    protected List<Employee> employee;

    /**
     * Gets the value of the employee property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the employee property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getEmployee().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link Employee }
     * 
     * 
     */
    public List<Employee> getEmployee() {
        if (employee == null) {
            employee = new ArrayList<Employee>();
        }
        return this.employee;
    }

}


Now let me explain one by one.
 <!-- Schema for element employees -->
 <xs:element name="employees">
  <xs:complexType>
   <xs:sequence>
                            <xs:element ref="employee" maxOccurs="unbounded" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>


As you see above snippet, employees element is the root element, that contains sequence (list) of employees. Following is the generated class.
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "employee"
})
@XmlRootElement(name = "employees")
public class Employees {

    @XmlElement(required = true)
    protected List<Employee> employee;

    public List<Employee> getEmployee() {
        if (employee == null) {
            employee = new ArrayList<Employee>();
        }
        return this.employee;
    }

}


Since employees is the root element, it is annotated with annotation ‘@XmlRootElement(name = "employees")’. ‘XmlRootElement’ annotation Maps a class or an enum type to an XML element.

Now lets see the schema for employee.
<xs:element name="employee">
  <xs:complexType>
   <xs:sequence>
                            <xs:element ref="id" />
                            <xs:element ref="description" />
                            <xs:element ref="taxExemption" />
                            <xs:element ref="taxPaid" />
                            <xs:element ref="descendants" />
                            <xs:element ref="empNum" />
                            <xs:element ref="specialities" />
                            <xs:element ref="satifiedWithWork" />
                            <xs:element ref="details" />
                            <xs:element ref="project" maxOccurs="unbounded" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>


Following is the generated class for above schema.
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "id",
    "description",
    "taxExemption",
    "taxPaid",
    "descendants",
    "empNum",
    "specialities",
    "satifiedWithWork",
    "details",
    "project"
})
@XmlRootElement(name = "employee")
public class Employee {

    @XmlElement(required = true)
    protected BigInteger id;
    @XmlElement(required = true)
    protected String description;
    protected float taxExemption;
    @XmlElement(required = true)
    protected BigDecimal taxPaid;
    protected int descendants;
    @XmlElement(required = true)
    protected BigInteger empNum;
    @XmlList
    @XmlElement(required = true)
    protected List<String> specialities;
    protected boolean satifiedWithWork;
    @XmlElement(required = true)
    protected Details details;
    @XmlElement(required = true)
    protected List<Project> project;

 /* Getter and setters are removed to save space */

}


1. <xs:element name="id" type="xs:integer" />

Above element is of type integer, it is mapped to BigInteger in Java

@XmlElement(required = true)
protected BigInteger id;

2. <!-- validate description -->
        <xs:element name="description">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:whiteSpace value="collapse" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>

Above element is mapped to string, it is mapped to String in Java.

@XmlElement(required = true)
protected String description;


3.         <!-- validate taxEmption -->
        <xs:element name="taxExemption">
            <xs:simpleType>
                <xs:restriction base="xs:float">
                    <xs:maxExclusive value="250000.1" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
     
element 'taxExemption' is mapped to float, so it is mapped to float in Java.

protected float taxExemption;

4.    
<!-- validate taxPaid -->
        <xs:element name="taxPaid">
            <xs:simpleType>
                <xs:restriction base="xs:decimal">
                    <xs:fractionDigits value="2" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>

taxPaid is of type decimal, so it is mapped to BigDecimal in Java.

@XmlElement(required = true)
protected BigDecimal taxPaid;

5.  <!-- validate descendents -->
        <xs:element name="descendants">
            <xs:simpleType>
                <xs:restriction base="xs:integer">
                    <xs:minInclusive value="1" />
                    <xs:maxInclusive value="7" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>

descendants is of type integer, so it ismapped to int in Java.

protected int descendants;

6.   <!-- validate mobileNum -->
        <xs:element name="empNum">
            <xs:simpleType>
                <xs:restriction base="xs:integer">
                    <xs:totalDigits value="9" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>

empNum is of type integer, so it ismapped to int in Java. 

@XmlElement(required = true)
protected BigInteger empNum;

7.  <!-- validate specialities -->
        <xs:element name="specialities">
            <xs:simpleType>
                <xs:list itemType="xs:string" />
            </xs:simpleType>
        </xs:element>

specialities is list of strings, so it is mapped to list of strings in Java.

@XmlList
@XmlElement(required = true)
protected List<String> specialities;
      
8.  <!-- validate satifiedWithWork -->
        <xs:element name="satifiedWithWork">
            <xs:simpleType>
                <xs:restriction base="xs:boolean">
                </xs:restriction>
            </xs:simpleType>
        </xs:element>

satifiedWithWork element is of type boolean, so it is mapped to boolean in Java.

protected boolean satifiedWithWork;

9. <!-- Schema for element details -->
         <xs:element name="details">
                  <xs:complexType>
                           <xs:sequence>
                            <xs:element ref="firstname" />
                            <xs:element ref="middlename" />
                            <xs:element ref="lastname" />
                            <xs:element ref="age" />
                            <xs:element ref="salary" />
                            <xs:element ref="married" />
                            <xs:element ref="sex" />
                            <xs:element ref="address" />
                           </xs:sequence>
                  </xs:complexType>
         </xs:element>

Details is of complex type, it is mapped to following class.

public class Details {

    @XmlElement(required = true)
    protected String firstname;
    @XmlElement(required = true)
    protected String middlename;
    @XmlElement(required = true)
    protected String lastname;
    protected int age;
    @XmlElement(required = true)
    protected BigDecimal salary;
    @XmlElement(required = true)
    protected String married;
    @XmlElement(required = true)
    protected String sex;
    @XmlElement(required = true)
    protected Address address;


}

10.  <!-- schema for address -->
        <xs:element name="address">
            <xs:complexType>
                <xs:sequence>
                    <xs:element ref="city" />
                    <xs:element ref="district" />
                    <xs:element ref="state" />
                    <xs:element ref="country" />
                    <xs:element ref="pin" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>

 address is of complex type it is mapped to following class.

 public class Address {

    @XmlElement(required = true)
    protected String city;
    @XmlElement(required = true)
    protected String district;
    @XmlElement(required = true)
    protected String state;
    @XmlElement(required = true)
    protected String country;
    @XmlElement(required = true)
    protected String pin;
}

Same applies for remaining all elements.





Previous                                                 Next                                                 Home

No comments:

Post a Comment