2009/4
JAXBとかcommons.digester等、XML←→Object変換するライブラリはたくさん
ありますが、実はJDKのクラスの中にも存在するのです。
それが
・java.beans.XMLEncoder
・java.beans.XMLDecoder
です。使い方は簡単です。コードを見てみましょう!!
スポンサーリンク
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
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
とかを使った方がいいかもしれません。