In this tutorial we show you how to generate XSD from Java Classes using java binding annotations. Java Classes can be converted into XSD schema and vice versa using the jaxb2-maven-plugin . We can specify which classes need to be included and use a post processor to bind a specific namespace to a filename. This plugin requires that your classes are annotated with JAXB annotations.
Generate XSD from Java Classes with Maven
With the help of the jaxb2-maven-plugin we can generate XSD schemas from Java Classes. By default it scans all the folders in src/main/java recursively for annotated JAX-B Java Classes. But for this example we specify the source destination of our JAX-B annotated Classes. We also register a transformSchemas which is a post processor that is responsible for naming the XSD schema. It works by matching the namespace with the namespace of the @XmlType of your Java Class. Once we will build our project, it will generate XSD classes in target/generated-resources/schemagen directory.
In order for a Java Class to be eligible for an XSD schema candidate, the class must be annotated with the @XmlType annotation. In this example we give the class a concrete namespace of https://memorynotfound.com/course this way we can uniquely identify the resource.
Once the project is build, it will generate XSD classes in target/generated-resources/schemagen directory. This XSD Schema is the result of the previous Java Class.
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Java cli tool for generating XSD from an XML
License
wiztools/xsd-gen
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Command-line tool written in Java for generating XSD from an XML. Can also be used as a library within your application.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
In last few posts, we learned about Java JAXB and how to generate java class from XSD. Today we will learn how to generate XSD from java classes.
Generate XSD from Java Class
We will use JAXB-2 Maven Plugin in a maven project to generate XSD from java classes.
JAXB2 Maven Plugin uses JAXB SchemaGenerator utility to generate XSD from java classes.
Java classes should have JAXB annotations to be used by this plugin.
Minimum java version required is Java 5
First create a new maven project, you can give any name, group id and artifact id you want. Once we will build our project, it will generate XSD classes in target/generated-resources/schemagen directory. After build, our project structure will look like below image. Here is the final pom.xml file we have:
Few things to notice are jaxb dependency, schemagen execution goal and transformSchema configuration. transformSchema configuration is used to specify the XSD file name generated and namespace prefix to be used in the XSD file. Here are the java classes we have that will be used to generate XSD. Employee.java
Notice the XmlType annotation with namespace used for the class and XmlAttribute for the field id. This class will generate employee.xsd schema once we build the project. As you can see that it has a field Address that is another custom class, so we need to annotate this class also for successful schema generation. Here is the address class with jaxb annotations. Address.java
This class will generate address.xsd because it’s name is matched in transformSchema in pom.xml file. Our project setup is ready, just build the project using command mvn clean install and the XSD files will be generated. For my project, generated XSD files are as below. employee.xsd
That’s all for generating XSD from java class. It’s very simple and great way for java class to XSD generation. I hope you will find it useful and easy to understand.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.