2005.10.14 株式会社四次元データ 小野彩子
9.3 クラスの生成
それでは、JAXBを使用して、XMLのスキーマからクラスを生成してみましょう。例として、従業員情報を取り上げます。従業員情報は、以下のようにXMLで表します。
employees.xml
<?xml version="1.0"?>
<employees>
<employee>
<name>山田太郎</name>
<nameKana>ヤマダタロウ</nameKana>
<section>開発部</section>
</employee>
<employee>
<name>海野次郎</name>
<nameKana>ウミノジロウ</nameKana>
<section>営業部</section>
</employee>
...
</employees>
この従業員情報のスキーマをXML Schemaで記述すると、以下のようになります。
employees.xsd
<?xml version="1.0" encoding="EUC-JP"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="1.0">
<xs:element name="employees">
<xs:complexType>
<xs:sequence>
<xs:element ref="employee" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="nameKana" type="xs:string"/>
<xs:element name="section" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
JAXBは、このようなXMLのスキーマから従業員を表すEmployeeクラスなどを自動生成します。自動生成されたクラスを使用すれば、JavaBeanを使うのと同じ感覚でXMLの編集が可能になります。クラスを生成するには、以下のコマンドを実行します。
$xjc.sh -p test.jaxb employees.xsd -d .
「xjc.sh」を使用して、スキーマからクラスを生成します。「xjc.sh」スクリプトは$JWSDP_HOME/jaxb/binディレクトリにあります。「-p」オプションでは、生成するクラスを格納するパッケージを指定します。ここでは、「test.jaxb」を指定しましたので、生成されるクラスは「test.jaxb」パッケージ、またはそのサブパッケージに存在することになります。次に、スキーマファイル名を指定します。最後に、生成するクラスファイルを格納するディレクトリを指定します。ここでは、カレントディレクトリに生成することにします。コマンドを実行すると、以下のように表示されます。
$xjc.sh -p test.jaxb employees -d . parsing a schema... compiling a schema... test/jaxb/impl/runtime/NamespaceContext2.java test/jaxb/impl/runtime/UnmarshallerImpl.java test/jaxb/impl/runtime/GrammarInfoFacade.java test/jaxb/impl/runtime/UnmarshallableObject.java test/jaxb/impl/runtime/ErrorHandlerAdaptor.java test/jaxb/impl/runtime/UnmarshallingEventHandlerAdaptor.java test/jaxb/impl/runtime/MarshallerImpl.java test/jaxb/impl/runtime/XMLSerializable.java test/jaxb/impl/runtime/AbstractUnmarshallingEventHandlerImpl.java test/jaxb/impl/runtime/DefaultJAXBContextImpl.java test/jaxb/impl/runtime/SAXUnmarshallerHandler.java test/jaxb/impl/runtime/Discarder.java test/jaxb/impl/runtime/UnmarshallingContext.java test/jaxb/impl/runtime/UnmarshallingEventHandler.java test/jaxb/impl/runtime/PrefixCallback.java test/jaxb/impl/runtime/ValidatingUnmarshaller.java test/jaxb/impl/runtime/ValidationContext.java test/jaxb/impl/runtime/ContentHandlerAdaptor.java test/jaxb/impl/runtime/SAXUnmarshallerHandlerImpl.java test/jaxb/impl/runtime/GrammarInfo.java test/jaxb/impl/runtime/SAXMarshaller.java test/jaxb/impl/runtime/NamespaceContextImpl.java test/jaxb/impl/runtime/MSVValidator.java test/jaxb/impl/runtime/Util.java test/jaxb/impl/runtime/ValidatableObject.java test/jaxb/impl/runtime/GrammarInfoImpl.java test/jaxb/impl/runtime/XMLSerializer.java test/jaxb/impl/runtime/ValidatorImpl.java test/jaxb/impl/EmployeeImpl.java test/jaxb/impl/EmployeeTypeImpl.java test/jaxb/impl/EmployeesImpl.java test/jaxb/impl/EmployeesTypeImpl.java test/jaxb/impl/JAXBVersion.java test/jaxb/Employee.java test/jaxb/EmployeeType.java test/jaxb/Employees.java test/jaxb/EmployeesType.java test/jaxb/ObjectFactory.java test/jaxb/jaxb.properties test/jaxb/bgm.ser
ずいぶんたくさんのクラスのソースファイルが生成されます。実際に操作を行うのは、コマンド実行時にオプションで指定したパッケージ「test.jaxb」に含まれる「Employee」や「Employees」です。Employeesクラスはemployees要素、Employeeクラスはemployee要素を表すクラスになっています。これらのクラスを使用して、XML文書を読み込んだり、作成したりする方法については、次回以降に説明します。
(実習課題2)
以下の商品情報を表すXMLのSchemaを作成しなさい。またJAXBを使用して、作成したスキーマよりクラスを生成しなさい。
product.xml
<?xml version="1.0"?>
<products>
<product>
<name>ゼロからはじめるJava</name>
<price>1905</price>
</product>
<product>
<name>ゼロからはじめるJ2EE</name>
<price>2095</price>
</product>
...
</products>

