Saturday 20 August 2016

jaxb: Generate XML schema from Java classes

In my previous post, I explained how to map xsd file to Java classes. In this post, I am going to explain how to generate xsd file from Java classes.

You can download xjc, schemagen (xjc includes schemagen) from following command.



Employee.java
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlType(name = "", propOrder = { "id", "firstName", "lastName", "address" })
@XmlRootElement(name = "employee")
public class Employee {
 private int id;
 private String firstName;
 private String lastName;
 private Address address;

 public Address getAddress() {
  return address;
 }

 public void setAddress(Address address) {
  this.address = address;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

}


Address.java
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlType(name = "", propOrder = { "street", "city", "country" })
@XmlRootElement(name = "address")
public class Address {
 private String city;
 private String street;
 private String country;

 public String getCity() {
  return city;
 }

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

 public String getStreet() {
  return street;
 }

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

 public String getCountry() {
  return country;
 }

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

}


Following step-by-step procedure explains how to get xsd file from java classes.

Step 1: Compile the classes.
I placed Employee.java and Address.java in the directory ‘/Users/harikrishna_gurram/Documents/’.

$ javac -d bin /Users/harikrishna_gurram/Documents/*.java

Step 2: Now type the following command to generate the schema from Java source files.

$ schemagen -cp bin /Users/harikrishna_gurram/Documents/*.java
warning: Supported source version 'RELEASE_7' from annotation processor 'com.sun.tools.internal.jxc.ap.SchemaGenerator' less than -source '1.8'
Note: Writing /Users/harikrishna_gurram/Documents/jaxb-ri/bin/bin/schema1.xsd

Following is the generated xsd file.

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

  <xs:element name="address">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="street" type="xs:string" minOccurs="0"/>
        <xs:element name="city" type="xs:string" minOccurs="0"/>
        <xs:element name="country" type="xs:string" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="employee">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="id" type="xs:int"/>
        <xs:element name="firstName" type="xs:string" minOccurs="0"/>
        <xs:element name="lastName" type="xs:string" minOccurs="0"/>
        <xs:element ref="address" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>




Previous                                                 Next                                                 Home

No comments:

Post a Comment