2009/4
JAXBとかcommons.digester等、XML←→Object変換するライブラリはたくさん
ありますが、実はJDKのクラスの中にも存在するのです。
それが
・java.beans.XMLEncoder
・java.beans.XMLDecoder
です。使い方は簡単です。コードを見てみましょう!!
package sample.xmlconvert; import java.beans.XMLDecoder; import java.beans.XMLEncoder; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; public class XMLConvertSample { public static void main(String[] args) throws FileNotFoundException { //値オブジェクトを作成 ValueObject1 vo1 = new ValueObject1(); vo1.setString1("Lust"); vo1.setInt1(139); vo1.setDouble1(56.7); ArrayList list = new ArrayList(); list.add("Greed"); list.add("Edo"); list.add(new Date()); ValueObject2 vo2 = new ValueObject2(); vo2.setMoney(new Integer("156488")); vo2.setName("Mastang"); vo2.setTax(1.546588f); list.add(vo2); vo1.setArrayList1(list); HashMap map = new HashMap(); map.put("key1", "Aru"); map.put("key2", "Winly"); map.put("key3", vo2); vo1.setHashMap1(map); //値オブジェクトをXMLに変換(Object → XML)してファイルに出力する。 XMLEncoder e = new XMLEncoder( new BufferedOutputStream(new FileOutputStream("Test.xml"))); e.writeObject(vo1); e.close(); //XMLを値オブジェクトに変換(XML → Object) XMLDecoder d = new XMLDecoder( new BufferedInputStream(new FileInputStream("Test.xml"))); Object result = d.readObject(); d.close(); //値オブジェクトをコンソールに出力する ValueObject1 resultVo1 = (ValueObject1) result; System.out.println("getDouble1=" + resultVo1.getDouble1()); System.out.println("getInt1=" + resultVo1.getInt1()); System.out.println("getString1=" + resultVo1.getString1()); System.out.println("getArrayList1=" + resultVo1.getArrayList1()); System.out.println("getHashMap1=" + resultVo1.getHashMap1()); } } package sample.xmlconvert; import java.util.ArrayList; import java.util.HashMap; public class ValueObject1 { private String string1; private int int1; private double double1; private ArrayList arrayList1; private HashMap hashMap1; public ArrayList getArrayList1() { return arrayList1; } public double getDouble1() { return double1; } public HashMap getHashMap1() { return hashMap1; } public int getInt1() { return int1; } public String getString1() { return string1; } public void setArrayList1(ArrayList list) { arrayList1 = list; } public void setDouble1(double d) { double1 = d; } public void setHashMap1(HashMap map) { hashMap1 = map; } public void setInt1(int i) { int1 = i; } public void setString1(String string) { string1 = string; } } package sample.xmlconvert; public class ValueObject2 { private String name; private float tax; private Integer money; public Integer getMoney() { return money; } public String getName() { return name; } public float getTax() { return tax; } public void setMoney(Integer integer) { money = integer; } public void setName(String string) { name = string; } public void setTax(float f) { tax = f; } }
[実行結果]
getDouble1=56.7
getInt1=139
getString1=Lust
getArrayList1=[Greed, Edo, Tue Apr 08 16:01:26 JST 2008,
sample.xmlconvert.ValueObject2@5be64f80]
getHashMap1={key1=Aru, key3=sample.xmlconvert.ValueObject2@5be64f80,
key2=Winly}
getInt1=139
getString1=Lust
getArrayList1=[Greed, Edo, Tue Apr 08 16:01:26 JST 2008,
sample.xmlconvert.ValueObject2@5be64f80]
getHashMap1={key1=Aru, key3=sample.xmlconvert.ValueObject2@5be64f80,
key2=Winly}
[Test.xml]
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.1" class="java.beans.XMLDecoder">
<object class="sample.xmlconvert.ValueObject1">
<void property="arrayList1">
<object class="java.util.ArrayList">
<void method="add">
<string>Greed</string>
</void>
<void method="add">
<string>Edo</string>
</void>
<void method="add">
<object class="java.util.Date">
<long>1207638086172</long>
</object>
</void>
<void method="add">
<object id="ValueObject20" class="sample.xmlconvert.ValueObject2">
<void property="money">
<int>156488</int>
</void>
<void property="name">
<string>Mastang</string>
</void>
<void property="tax">
<float>1.546588</float>
</void>
</object>
</void>
</object>
</void>
<void property="double1">
<double>56.7</double>
</void>
<void property="hashMap1">
<object class="java.util.HashMap">
<void method="put">
<string>key1</string>
<string>Aru</string>
</void>
<void method="put">
<string>key3</string>
<object idref="ValueObject20"/>
</void>
<void method="put">
<string>key2</string>
<string>Winly</string>
</void>
</object>
</void>
<void property="int1">
<int>139</int>
</void>
<void property="string1">
<string>Lust</string>
</void>
</object>
</java>
<java version="1.4.1" class="java.beans.XMLDecoder">
<object class="sample.xmlconvert.ValueObject1">
<void property="arrayList1">
<object class="java.util.ArrayList">
<void method="add">
<string>Greed</string>
</void>
<void method="add">
<string>Edo</string>
</void>
<void method="add">
<object class="java.util.Date">
<long>1207638086172</long>
</object>
</void>
<void method="add">
<object id="ValueObject20" class="sample.xmlconvert.ValueObject2">
<void property="money">
<int>156488</int>
</void>
<void property="name">
<string>Mastang</string>
</void>
<void property="tax">
<float>1.546588</float>
</void>
</object>
</void>
</object>
</void>
<void property="double1">
<double>56.7</double>
</void>
<void property="hashMap1">
<object class="java.util.HashMap">
<void method="put">
<string>key1</string>
<string>Aru</string>
</void>
<void method="put">
<string>key3</string>
<object idref="ValueObject20"/>
</void>
<void method="put">
<string>key2</string>
<string>Winly</string>
</void>
</object>
</void>
<void property="int1">
<int>139</int>
</void>
<void property="string1">
<string>Lust</string>
</void>
</object>
</java>
ArrayListやHashMapも変換できるようです。
しかし、BigIntegerは駄目でした。変換させようとすると
下記のようにコンソールに出力されXMLにはBigIntegerの値は出力されません。
[実行結果]
java.lang.InstantiationException: java.math.BigInteger
Continuing ...
java.lang.RuntimeException: failed to evaluate: <unbound>=BigInteger.new();
Continuing ...
Continuing ...
java.lang.RuntimeException: failed to evaluate: <unbound>=BigInteger.new();
Continuing ...
ってことで、やっぱりJAXBとかcommons.digester
とかを使った方がいいかもしれません。