xsd 生成 java 类
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.
在最近的几篇文章中,我们了解了Java JAXB以及如何从XSD生成Java类 。 今天,我们将学习如何从Java类生成XSD。
从Java类生成XSD (Generate XSD from Java Class)
We will use JAXB-2 Maven Plugin in a maven project to generate XSD from java classes.
我们将在maven项目中使用JAXB-2 Maven插件从Java类生成XSD。
- JAXB2 Maven Plugin uses JAXB SchemaGenerator utility to generate XSD from java classes. JAXB2 Maven插件使用JAXB SchemaGenerator实用程序从Java类生成XSD。
- Java classes should have JAXB annotations to be used by this plugin. Java类应具有此插件要使用的JAXB批注。
- Minimum java version required is Java 5 所需的最低Java版本是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.
首先创建一个新的Maven项目,您可以提供所需的任何名称,组ID和工件ID。 构建项目后,它将在target/generated-resources/schemagen
目录中生成XSD类。
After build, our project structure will look like below image.
构建后,我们的项目结构将如下图所示。
Here is the final pom.xml
file we have:
这是我们拥有的最终pom.xml
文件:
4.0.0jaxb-schemagenjaxb-schemagen0.0.1-SNAPSHOTjavax.xml.bindjaxb-api2.1org.apache.maven.pluginsmaven-compiler-plugin2.5.1org.codehaus.mojojaxb2-maven-plugin1.5schemagenschemagenhttps://www.example.org/employeeempnsemployee.xsdhttps://www.example.org/addressaddrnsaddress.xsdcom/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.
很少注意的是jaxb
依赖关系, schemagen
执行目标和transformSchema
配置。 transformSchema配置用于指定生成的XSD文件名和要在XSD文件中使用的名称空间前缀。
Here are the java classes we have that will be used to generate XSD.
这是我们将用于生成XSD的Java类。
Employee.java
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;}@XmlAttributepublic 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.
请注意, XmlType
批注带有用于类的名称空间,而XmlAttribute
用于字段ID。 一旦我们构建项目,该类将生成employee.xsd
模式。
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.
如您所见,它的字段Address是另一个自定义类,因此为了成功生成模式,我们还需要注释该类。
Here is the address class with jaxb annotations.
这是带有jaxb批注的地址类。
Address.java
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.
此类将生成address.xsd,因为其名称在pom.xml文件的transformSchema中匹配。
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.
我们的项目设置已准备就绪,只需使用命令mvn clean install
构建项目,然后将生成XSD文件。 对于我的项目,生成的XSD文件如下。
employee.xsd
employee.xsd
address.xsd
address.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.
这就是从java类生成XSD的全部。 这是将Java类生成XSD的非常简单且绝佳的方法。 我希望您会发现它有用且易于理解。
翻译自: https://www.journaldev.com/1316/generate-xsd-from-java-class
xsd 生成 java 类