- Generate XSD from Java Classes
- Generate XSD from Java Classes with Maven
- Java Class to be Transformed into XSD Schema
- Generated XSD
- References
- Saved searches
- Use saved searches to filter your results more quickly
- License
- wiztools/xsd-gen
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- How to generate XSD from Java Class
- Generate XSD from Java Class
- Generate XSD from JAXB Java Classes using Eclipse
- 2.1) Navigate to Eclipse Option
- 2.3) Select JAXB Classes
- 2.4) Generate xsd files
- How to generate xml schema xsd from java class using jaxb in eclipse
- Steps:
- On Console:
Generate XSD from Java Classes
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.
4.0.0 com.memorynotfound.xml generate-xsd-from-java 1.0.0-SNAPSHOT XSD - $ UTF-8 UTF-8 org.codehaus.mojo jaxb2-maven-plugin 2.2 schemagen schemagen https://memorynotfound.com/course c course.xsd
Java Class to be Transformed into XSD Schema
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.
package com.memorynotfound; import javax.xml.bind.annotation.*; import java.util.Date; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "Course", propOrder = < "id", "name", "description", "created" >, namespace = "https://memorynotfound.com/course") public class Course < protected int id; @XmlElement(required = true) protected String name; @XmlElement(required = true) protected String description; @XmlElement(required = true) @XmlSchemaType(name = "dateTime") protected Date created; public int getId() < return id; >public void setId(int id) < this.id = id; >public String getName() < return name; >public void setName(String name) < this.name = name; >public String getDescription() < return description; >public void setDescription(String description) < this.description = description; >public Date getCreated() < return created; >public void setCreated(Date created) < this.created = created; >>
Generated XSD
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.
References
Saved searches
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.
Mac users can install via homebrew:
brew tap wiztools/repo brew install xsd-gen
Once installed, xsd-gen is available in PATH .
$ java -jar xsd-gen-fat-VERSION.jar /path/to/xml.xml > /path/to/my.xsd
To view additional command-line options:
$ java -jar xsd-gen-fat-VERSION.jar -h
Latest versions of the library are available in the jitpack repository.
import org.wiztools.xsdgen.XsdGen; import java.io.File; import java.io.FileOutputStream; . XsdGen gen = new XsdGen(); gen.parse(new File("in.xml")); File out = new File("out.xsd"); gen.write(new FileOutputStream(out));
About
Java cli tool for generating XSD from an XML
How to generate XSD from Java Class
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:
4.0.0 jaxb-schemagen jaxb-schemagen 0.0.1-SNAPSHOT javax.xml.bind jaxb-api 2.1 org.apache.maven.plugins maven-compiler-plugin 2.5.1 org.codehaus.mojo jaxb2-maven-plugin 1.5 schemagen schemagen https://www.example.org/employee empns employee.xsd https://www.example.org/address addrns address.xsd com/journaldev/bean/* true
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
package com.journaldev.bean; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlType; @XmlType(namespace = "https://www.example.org/employee") public class Employee < private String name; private int id; private String role; private Address address; public String getName() < return name; >public void setName(String name) < this.name = name; >@XmlAttribute public int getId() < return id; >public void setId(int id) < this.id = id; >public String getRole() < return role; >public void setRole(String role) < this.role = role; >public Address getAddress() < return address; >public void setAddress(Address address) < this.address = address; >>
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
package com.journaldev.bean; import javax.xml.bind.annotation.XmlType; @XmlType(namespace = "https://www.example.org/address") public class Address < private String city; private int zip; private String addressLine1; private String addressLine2; public String getCity() < return city; >public void setCity(String city) < this.city = city; >public int getZip() < return zip; >public void setZip(int zip) < this.zip = zip; >public String getAddressLine1() < return addressLine1; >public void setAddressLine1(String addressLine1) < this.addressLine1 = addressLine1; >public String getAddressLine2() < return addressLine2; >public void setAddressLine2(String addressLine2) < this.addressLine2 = addressLine2; >>
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.
Generate XSD from JAXB Java Classes using Eclipse
Learn to create XML schema document (xsd) from JAXB annotated Java classes using Eclipse IDE.
1) Add JAXB Annotations to Java Classes
First step is to add annotations such as @XmlRootElement , @XmlAccessorType and @XmlElement etc. to your Java classes.
import java.io.Serializable; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "employee") @XmlAccessorType(XmlAccessType.PROPERTY) public class Employee implements Serializable < private static final long serialVersionUID = 1L; private Integer id; private String firstName; private String lastName; private Department department; public Employee() < super(); >//Setters and Getters >
import java.io.Serializable; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "department") @XmlAccessorType(XmlAccessType.PROPERTY) public class Department implements Serializable < private static final long serialVersionUID = 1L; Integer id; String name; public Department() < super(); >//Setters and Getters >
2) Generate XSD from JAXB Classes
2.1) Navigate to Eclipse Option
File -> New -> JAXB -> Schema from JAXB Classes
2.2) Select location for generated schema file
2.3) Select JAXB Classes
2.4) Generate xsd files
XSd file has been generated and now you can use it for various application usecases.
How to generate xml schema xsd from java class using jaxb in eclipse
Let us discuss how to generate or create xml schema xsd from java class using jaxb in eclipse with below example.
Steps:
1. Create a new JAXB project. File -> New -> Other -> JAXB -> JAXB Project.
2. Enter the project name and click on Finish button.
3. Download JAXB Jar and JAXB-XJC jar files and include in class path.
4. Create package which will contain xsd file.
5. Create package which will contain java classes.
6. Create a java class. Here we are using the java classes generated from previous example.
7. Right click on your package -> New -> Other -> JAXB -> Schema from JAXB Classes and click on Next.
8. Specify file name and location for new xsd file and click on Next.
9. Select java classes from which schema have to generated and click on Finish.
On Console:
loading. com.w3spoint.javaclass.Address com.w3spoint.javaclass.ObjectFactory com.w3spoint.javaclass.Student generating schema. Schema D:\TestWorkspace\JAXBXSDTest2\src\com\w3spoint\xsd\test1.xsd generated
loading. com.w3spoint.javaclass.Address com.w3spoint.javaclass.ObjectFactory com.w3spoint.javaclass.Student generating schema. Schema D:\TestWorkspace\JAXBXSDTest2\src\com\w3spoint\xsd\test1.xsd generated