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:

  • 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:

  • 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 *