如何从Java类生成XSD文件

在前面的几篇文章中,我们学习了关于Java JAXB以及如何从XSD生成Java类。今天我们将学习如何从Java类生成XSD。

从Java类生成XSD。

我们将在一个Maven项目中使用JAXB-2 Maven插件,通过Java类生成XSD。

  • 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

首先创建一个新的Maven项目,您可以任意指定名称、组ID和工件ID。一旦我们构建了项目,它将在target/generated-resources/schemagen目录中生成XSD类。构建完成后,我们的项目结构将与下图类似。这是我们最终的pom.xml文件:

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>jaxb-schemagen</groupId>
	<artifactId>jaxb-schemagen</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>javax.xml.bind</groupId>
			<artifactId>jaxb-api</artifactId>
			<version>2.1</version>
		</dependency>
	</dependencies>

	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-compiler-plugin</artifactId>
					<version>2.5.1</version>
				</plugin>
			</plugins>
		</pluginManagement>

		<plugins>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>jaxb2-maven-plugin</artifactId>
				<version>1.5</version>
				<executions>
					<execution>
						<id>schemagen</id>
						<goals>
							<goal>schemagen</goal>
						</goals>
					</execution>
				</executions>

				<configuration>
					<transformSchemas>
						<transformSchema>
							<uri>https://www.example.org/employee</uri>
							<toPrefix>empns</toPrefix>
							<toFile>employee.xsd</toFile>
						</transformSchema>
						<transformSchema>
							<uri>https://www.example.org/address</uri>
							<toPrefix>addrns</toPrefix>
							<toFile>address.xsd</toFile>
						</transformSchema>
					</transformSchemas>
					<includes>
						<include>com/scdev/bean/*</include>
					</includes>
					<verbose>true</verbose>

				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

需要注意的几个事项包括jaxb依赖项、schemagen执行目标和transformSchema配置。transformSchema配置用于指定生成的XSD文件名称和在XSD文件中使用的命名空间前缀。以下是我们拥有的将用于生成XSD的Java类:Employee.java

package com.Olivia.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;
    }
}

请注意用于类的带有命名空间的XmlType注释以及用于字段id的XmlAttribute注释。一旦我们构建项目,这个类将会生成employee.xsd模式。正如你所见,它有一个字段Address,它是另一个定制类,所以我们需要为成功生成模式对这个类进行注释。这是带有jaxb注释的address类。Address.java

package com.Olivia.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;
    }
    
}

这个班级将生成address.xsd文件,因为它在pom.xml文件的transformSchema中找到了匹配的名称。我们的项目设置已经准备就绪,只需要使用mvn clean install命令构建项目,XSD文件将会生成。对于我的项目,生成的XSD文件如下,其中包括employee.xsd。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="https://www.w3.org/2001/XMLSchema" xmlns:addrns="https://www.example.org/address" targetNamespace="https://www.example.org/employee" version="1.0">

  <xs:import namespace="https://www.example.org/address" schemaLocation="address.xsd"/>

  <xs:complexType name="employee">
    <xs:sequence>
      <xs:element minOccurs="0" name="address" type="ns1:address"/>
      <xs:element minOccurs="0" name="name" type="xs:string"/>
      <xs:element minOccurs="0" name="role" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name="id" type="xs:int" use="required"/>
  </xs:complexType>
</xs:schema>

地址.xsd

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="https://www.w3.org/2001/XMLSchema" targetNamespace="https://www.example.org/address" version="1.0">

  <xs:complexType name="address">
    <xs:sequence>
      <xs:element minOccurs="0" name="addressLine1" type="xs:string"/>
      <xs:element minOccurs="0" name="addressLine2" type="xs:string"/>
      <xs:element minOccurs="0" name="city" type="xs:string"/>
      <xs:element name="zip" type="xs:int"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

生成XSD从Java类就是如此。这是Java类生成XSD的简单而好的方式。希望您会发现它有用且易于理解。

发表回复 0

Your email address will not be published. Required fields are marked *


广告
将在 10 秒后关闭
bannerAds