从 XSD 生成 Java 类的 jaxb2-maven-plugin XJC 示例

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

jaxb2-maven 插件

我们使用 XSD 来定义合同数据结构,所以不常生成代表 XML 方案的 java 类. jaxb2-maven-plugin XJC 是 JAXB 绑定编译工具,可以用来从 XSD 文件生成 Java 类. 在这里,我们将学习如何在 maven 项目中使用 jaxb2-maven-plugin XJC 来生成 Java 类。

 1<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">
 2  <modelVersion>4.0.0</modelVersion>
 3  <groupId>jd</groupId>
 4  <artifactId>jd</artifactId>
 5  <version>0.0.1-SNAPSHOT</version>
 6
 7  <name>JD Example XSD to Java</name>
 8
 9    <properties>
10    	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11    </properties>
12    <build>
13    	<plugins>
14    		<!-- Plugin required to build java classes from XSD using XJC -->
15    		<plugin>
16    			<groupId>org.codehaus.mojo</groupId>
17    			<artifactId>jaxb2-maven-plugin</artifactId>
18    			<version>1.5</version>
19    			<executions>
20    				<execution>
21    					<goals>
22    						<goal>xjc</goal>
23    					</goals>
24    				</execution>
25    			</executions>
26    			<configuration>
27                   <!-- The name of your generated source package -->
28                	<arguments>-extension -npa -b ${project.basedir}/src/main/xsd/global.xjb</arguments> 
29                </configuration>
30    		</plugin>
31    		
32    	</plugins>
33    </build>
34    
35</project>

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

 1<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 2<jaxb:bindings version="2.0"
 3  xmlns:jaxb="https://java.sun.com/xml/ns/jaxb"
 4  xmlns:xjc="https://java.sun.com/xml/ns/jaxb/xjc"
 5  xmlns:xs="https://www.w3.org/2001/XMLSchema"
 6  jaxb:extensionBindingPrefixes="xjc">
 7
 8 <jaxb:globalBindings>
 9    <xjc:simple />
10    <xjc:serializable uid="-1" />
11    <jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime"
12      parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
13      printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />
14  </jaxb:globalBindings>
15</jaxb:bindings>

以下是将用于生成Java类的XSD。

 1<?xml version="1.0" encoding="UTF-8"?>
 2<schema xmlns="https://www.w3.org/2001/XMLSchema"
 3    targetNamespace="https://www.journaldev.com/com/journaldev/employee/data"
 4    xmlns:empns="https://www.journaldev.com/com/journaldev/employee/data"
 5    elementFormDefault="qualified">
 6
 7    <element name="empRequest" type="empns:EmpRequest"></element>
 8    <element name="empResponse" type="empns:EmpResponse"></element>
 9
10    <complexType name="EmpRequest">
11    	<sequence>
12    		<element name="id" type="int" minOccurs="0" maxOccurs="1" />
13    		<element name="name" type="string" minOccurs="0" maxOccurs="1" />
14    	</sequence>
15    </complexType>
16    
17    <complexType name="EmpResponse">
18    	<sequence>
19    		<element name="id" type="int" minOccurs="1" maxOccurs="1" />
20    		<element name="name" type="string" minOccurs="1" maxOccurs="1" />
21    		<element name="role" type="string" minOccurs="1" maxOccurs="unbounded" />
22    		<element name="gender" type="string" minOccurs="1" maxOccurs="1" />
23    		<element name="salary" type="string" minOccurs="1" maxOccurs="1" />
24    	</sequence>
25    </complexType>
26
27</schema>

Just build the maven project using mvn clean install and you will see java classes generated in target/generated-sources/jaxb directory. Finally the project would look something like below image. jaxb2-maven-plugin XJC Example Further Read: JAXB Tutorial Reference: jaxb2 maven plugin official page, java xjc

Published At
Categories with 技术
Tagged with
comments powered by Disqus