jaxb2-maven-plugin XJC示例,用于从XSD生成Java类

时间:2020-02-23 14:35:18  来源:igfitidea点击:

今天,我们将研究jaxb2-maven-plugin XJC示例,以从XSD生成Java类。
JAXB是Java类和XML之间的中间件技术。
我们可以使用JAXB从Java Object生成XML,反之亦然。

jaxb2-maven-插件

我们使用XSD定义合同数据结构,因此生成代表XML模式的Java类并不常见。

jaxb2-maven-plugin XJC是JAXB绑定编译器工具,可用于从XSD文件生成Java类。

其中我们将学习如何在Maven项目中使用jaxb2-maven-plugin XJC从XSD生成Java类。

首先,我们必须创建一个maven项目,然后需要使用jaxb2-maven-plugin插件从XSD生成Java类。

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>jd</groupId>
<artifactId>jd</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>JD Example XSD to Java</name>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<build>
		<plugins>
			<!-- Plugin required to build java classes from XSD using XJC -->
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>jaxb2-maven-plugin</artifactId>
				<version>1.5</version>
				<executions>
					<execution>
						<goals>
							<goal>xjc</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
                 <!-- The name of your generated source package -->
              	<arguments>-extension -npa -b ${project.basedir}/src/main/xsd/global.xjb</arguments> 
              </configuration>
			</plugin>
			
		</plugins>
	</build>
	
</project>

注意参数中的global.xjb文件,这是我们指定Java绑定规则的地方。

global.xjb

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="2.0"
xmlns:jaxb="https://java.sun.com/xml/ns/jaxb"
xmlns:xjc="https://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xs="https://www.w3.org/2001/XMLSchema"
jaxb:extensionBindingPrefixes="xjc">

 <jaxb:globalBindings>
  <xjc:simple 
  <xjc:serializable uid="-1" 
  <jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime"
    parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
    printMethod="javax.xml.bind.DatatypeConverter.printDateTime" 
</jaxb:globalBindings>
</jaxb:bindings>

这是将用于生成Java类的XSD。

Employee.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="https://www.w3.org/2001/XMLSchema"
	targetNamespace="https://www.theitroad.local/com/theitroad/employee/data"
	xmlns:empns="https://www.theitroad.local/com/theitroad/employee/data"
	elementFormDefault="qualified">

	<element name="empRequest" type="empns:EmpRequest"></element>
	<element name="empResponse" type="empns:EmpResponse"></element>

	<complexType name="EmpRequest">
		<sequence>
			<element name="id" type="int" minOccurs="0" maxOccurs="1" 
			<element name="name" type="string" minOccurs="0" maxOccurs="1" 
		</sequence>
	</complexType>
	
	<complexType name="EmpResponse">
		<sequence>
			<element name="id" type="int" minOccurs="1" maxOccurs="1" 
			<element name="name" type="string" minOccurs="1" maxOccurs="1" 
			<element name="role" type="string" minOccurs="1" maxOccurs="unbounded" 
			<element name="gender" type="string" minOccurs="1" maxOccurs="1" 
			<element name="salary" type="string" minOccurs="1" maxOccurs="1" 
		</sequence>
	</complexType>

</schema>

只需使用mvn clean install来构建maven项目,您将在target/generated-sources/jaxb目录中看到生成的Java类。