schemagen – Convert Java classes to XML Schema

Introduction

The schemagen tool converts Java classes to a XML Schema. This article is a quick reference page to use schemagen from both the command line and Maven.

This reference page is part of a series of articles on JAXB:

Command Line Syntax

The schemagen syntax below generates an .xsd XML Schema file for the Company class and dependants.

You could also use the -d option to define the location where the .xsd file will be created.

The schemagen tool can generate a XML Schema from either .java or .class files. If you generate it from .java files, you need to be aware that it will compile and leave the .class files in the source directory. This is the syntax:

For more details on the schemagen syntax check the official documentation.

Maven Configuration

If you use maven, you can use the JAXB-2 Maven Plugin to generate the XML Schema files. This is how you declare the plugin in the pom.xml file:

The above configuration generates an XML Schema for all classes in the com.actimem.blog.jaxb.schemagen package.  It also defines that all classes for namespace http://actimem.com/blog/jaxb/company are prefixed with letter “c“.  The company.xsd file will be generated in the target/generated-resources/schemagen directory.

Bibliography

The following two tabs change content below.

Eduard Manas

Eduard is a senior IT consultant with over 15 years in the financial sector. He is an experienced developer in Java, C#, Python, Wordpress, Tibco EMS/RV, Oracle, Sybase and MySQL.Outside of work, he likes spending time with family, friends, and watching football.

Latest posts by Eduard Manas (see all)

One thought on “schemagen – Convert Java classes to XML Schema”

  1. Hi,
    What will be the syntax to genearte XSD from all .class files in one go? For source code it works with *.java but doesn’t work for .class files.
    I don’t want to put individual class names as I got plenty of to play with.

Leave a Reply