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:
- Classes inside com.javahonk package will be generated by XSD file::
- You could also user below command to generate Java Classes:
xjc -d ../../../src/main/java -p com.javahonk JavaHonk.xsd
- Generated classes:
- 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:
Download project: XSDToJava
For more information please visit Oracle documentation here