Generate XML XSD Java JAXB Example

Generate XML XSD Java JAXB Example

In previous tutorial you saw how to generate Java class from XSD file. In this tutorial I will show you complete example of generating classes start from XML –> XSD –> Java Classes –> XML.

Note: Please see this tutorial to generate XSD from XML file and below is XSD which we will use to generate Java Classes and finally XML where data will be populated dynamically.

Tools: You don’t need anything extra except Eclipse latest version and JDK 1.8.

  • XML file:

  • Converted XSD File from above XML:

  • To understand better I will create maven project (XSDToJava) and below is final structure:

Generate XML XSD Java JAXB Example

  • Classes inside com.javahonk package will be generated by XSD file::

Generate XML XSD Java JAXB Example

  • You could also user below command to generate Java Classes:

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

  • Generated classes:

Generate XML XSD Java JAXB Example

  • Now we will populate some data to Java Object and finally will generate XML string using JavaToXML.java class:

  • To run right click JavaToXML.java –> Run As –> Java Application you will see below output on console:

Generate XML XSD Java JAXB Example

Download project: XSDToJava

For more information please visit Oracle documentation here

Leave a Reply

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