XSD Java Class Conversion example

XSD Java Class Conversion example

XSD (XML Schema Definition) is W3C recommendation which specifies how to formally describe elements in an XML (Extensible Markup Language) document. Its very popular and preferable approach and to create an universal document which is language independent. Many companies you will see they use XSD to describe elements which can be use to send message, exchange information, validate data’s etc… One of the requirement I was working where I had to send message to downstream system using Java and I got XSD document from business people. First thing I had to generate Java classes from XSD document then populated Java object with value and convert Java to XML and send message over to downstream system. In this tutorial I will show how to convert Java classed from XSD document. Next tutorial will convert conversion of Java classed into XML.

If you get XSD document from somebody then its easy to generate Java classes. But its good have knowledge about how to convert XSD document. Wikipedia has complete comparison list of XML editor tool here. In this list many are commercial and some are free. If you are in hurry like me then just create XML document by yourself then generate XSD online using free tool. Below is my XML:

<?xml version="1.0"?>
<Company>
  <Employee>
      <FirstName></FirstName>
      <LastName></LastName>
      <ContactNo></ContactNo>
      <Email></Email>
      <Address>
           <City></City>
           <State></State>
           <Zip></Zip>
      </Address>
  </Employee>
</Company>
  • Now to convert above XML I found good online tool here and this is sufficeint to start with XSD. Just copy and paste above XML to this tool you will see below XSD got generated:

Note: While generating using above please select Venetian Blind design:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
  <xs:element name="Company" type="CompanyType"/>
  <xs:complexType name="AddressType">
    <xs:sequence>
      <xs:element type="xs:string" name="City"/>
      <xs:element type="xs:string" name="State"/>
      <xs:element type="xs:string" name="Zip"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="EmployeeType">
    <xs:sequence>
      <xs:element type="xs:string" name="FirstName"/>
      <xs:element type="xs:string" name="LastName"/>
      <xs:element type="xs:string" name="ContactNo"/>
      <xs:element type="xs:string" name="Email"/>
      <xs:element type="AddressType" name="Address"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="CompanyType">
    <xs:sequence>
      <xs:element type="EmployeeType" name="Employee"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
  • Copy generated XSD to your workspace. Now to generate Java classed right click XSD file –> Generate –> JAXB Classes then follow onscreen instruction:

XSD Java Class Conversion example

XSD Java Class Conversion example

XSD Java Class Conversion example

XSD Java Class Conversion example

XSD Java Class Conversion exampleXSD Java Class Conversion example

  • You will see below classes got generated:

XSD Java Class Conversion example

  • If you refresh your Eclipse workspace you will see all classed is in inside of com.javahonk pakcages:

XSD Java Class Conversion example

Using command line: Run following below command from your eclipse workspace or command line window:

xjc -d ../../../src/main/java -p com.javahonk JavaHonk.xsd

If your XSD file is inside folder then use below:

xjc -d ../../../src/main/java -p com.javahonk ./Folder_Name/JavaHonk.xsd

Leave a Reply

Your email address will not be published. Required fields are marked *