「Java」Castor でマーシャル アンマーシャル

Castor 使ってマーシャル/アンマーシャルやってみた。その時のメモ。

[ 環境情報 ]
Windows 7 SP1
Eclipse 4.2
Castor 1.3.3 ( 面倒だったのでダウンロードしてきた jar ファイル全てをクラスパスに通しました )

※ 依存関係のあるライブラリは以下に記載がある ( とりあえず Commons logging 1.1.3 と lang 2.6 入れたら動きました )。

・3rd-party product dependencies
http://castor.codehaus.org/dependencies.html


■ XMLCastorTest.java

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
import org.exolab.castor.xml.XMLContext;

public class XMLCastorTest {

	private Mapping mapping = null;
	private XMLContext context = null;

	public XMLCastorTest() {
		try {
			mapping = new Mapping();
			mapping.loadMapping("castor\\mapping.xml");
			context = new XMLContext();
			context.addMapping(mapping);
		} catch (MappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		XMLCastorTest inst = new XMLCastorTest();
		inst.marshalTest();
		//inst.unmarshalTest();
	}

	private void marshalTest() {
		FileWriter fw = null;
		try {
			Person person = new Person(1, "hoge", 20, "090-0000-0000", "def");

			fw = new FileWriter("castor\\output.xml");
			Marshaller marshaller = context.createMarshaller();
			marshaller.setWriter(fw);
			marshaller.marshal(person);

		} catch (IOException e) {
			e.printStackTrace();
		} catch (ValidationException e) {
			e.printStackTrace();
		} catch (MarshalException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fw != null) {
					fw.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void unmarshalTest() {
		FileReader fr = null;
		try {
			fr = new FileReader("castor\\input.xml");
			Unmarshaller unmarshaller = context.createUnmarshaller();
			Person person = (Person) unmarshaller.unmarshal(fr);

			System.out.println("Name : " + person.getName());
			System.out.println("Age : " + person.getAge());
			System.out.println("Tel : " + person.getTel());
			System.out.println("Id : " + person.getId());
			System.out.println("abc : " + person.getAbc());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ValidationException e) {
			e.printStackTrace();
		} catch (MarshalException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fr != null) {
					fr.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}


■ Person.java

public class Person {
	private int id;
	private String name;
	private int age;
	private String tel;
	private String abc;

	// デフォルトコンストラクタ用意しとかないと怒られました・・・
	public Person() {

	}

	public Person(int id, String name, int age, String tel, String abc) {
		this.id = id;
		this.name = name;
		this.age = age;
		this.tel = tel;
		this.abc = abc;
	}
	
	// それぞれの getter,setter メソッド


■ mapping.xml

<?xml version="1.0" encoding="UTF-8"?>
<mapping>
<class name="test.castor.Person">
<map-to xml="person"/>
   <field name="id" type="integer">
      <bind-xml name="id" node="attribute"/>
   </field>
   <field name="name" type="string">
      <bind-xml name="name" node="element"/>
   </field>
   <field name="age" type="integer">
      <bind-xml name="age" node="element"/>
   </field>
   <field name="tel" type="string">
      <bind-xml name="tel" node="element"/>
   </field>
   <!-- name 要素に abc 属性を持たせる -->
   <field name="abc" type="string">
      <bind-xml name="abc" node="attribute" location="name"/>
   </field>
</class>
</mapping>


■ input.xml ( アンマーシャル用 )

<?xml version="1.0" encoding="UTF-8"?>
<person id="2">
  <name abc="def">uga</name>
  <age>30</age>
  <tel>090-0000-0000</tel>
</person>


■ マーシャルの結果 ( output.xml )

<?xml version="1.0" encoding="UTF-8"?>
<person id="1"><name abc="def">hoge</name><age>20</age><tel>090-0000-0000</tel></person>


■ アンマーシャルの結果

Name : uga
Age : 30
Tel : 090-0000-0000
Id : 2
abc : def


ドキュメント見てるともっといろんな事ができるんでしょうが、基本的なところは動かせたと思うのでひとまず終了。

Castor 1.3.3 - Reference documentation
http://castor.codehaus.org/reference/1.3.3/html/

以下あたりにもいろいろ書いてますね。

Castor によるデータ・バインディング: 第 3 回 スキーマ間のマッピング
http://www.ibm.com/developerworks/jp/xml/library/x-xjavacastor3/


以上です。