XSDからJavaクラスを生成するためのjaxb2-maven-plugin XJCの例を示します。

今日は、jaxb2-maven-pluginのXJCの例を見て、XSDからJavaクラスを生成する方法を見ていきます。JAXBはJavaクラスとXMLの間のミドルウェア技術です。JAXBを使ってJavaオブジェクトからXMLを生成したり、逆にXMLからJavaオブジェクトを生成することができます。

jaxb2-maven-pluginを日本語で言い換えると、次のようになります:「jaxb2-maven-plugin」

XSDを使用して契約データ構造を定義するため、XMLスキーマを表すJavaクラスを生成することは一般的ではありません。jaxb2-maven-plugin XJCは、XSDファイルからJavaクラスを生成するために使用できるJAXBバインディングコンパイラツールです。ここでは、mavenプロジェクトでjaxb2-maven-plugin XJCを使用してXSDからJavaクラスを生成する方法を学びます。まず、mavenプロジェクトを作成する必要があります。次に、pom.xmlでjaxb2-maven-pluginプラグインを使用してXSDからJavaクラスを生成する必要があります。

<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>

arguments にある global.xjb ファイルに注意してください。ここでは、Java バインディングのルールを指定します。

<?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.scdev.com/com/scdev/employee/data"
	xmlns:empns="https://www.scdev.com/com/scdev/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>

Mavenプロジェクトを「mvn clean install」コマンドを使用してビルドすると、target/generated-sources/jaxbディレクトリにJavaクラスが生成されます。最終的に、このプロジェクトは下の画像のように見えるでしょう。さらに読む: JAXBチュートリアル参照: jaxb2 mavenプラグイン公式ページ、java xjc

コメントを残す 0

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