如何读取xml并传入Hashtable?

时间:2014-04-29 15:58:46

标签: java xml hashtable

我用Java编程,我有这个代码将Hashtable传递给xml文件。

FileOutputStream fos = new FileOutputStream("example.xml");
XMLEncoder e = new XMLEncoder(fos);
e.writeObject(myHashTable);
e.close();

然后我的xml是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.8.0" class="java.beans.XMLDecoder">
 <object class="java.util.Hashtable">
  <void method="put">
   <string>D</string>
   <string>1011</string>
  </void>
  <void method="put">
   <string>C</string>
   <string>1001</string>
  </void>
  <void method="put">
   <string>B</string>
   <string>1</string>
  </void>
 </object>
</java>

我的问题是:我需要做些什么才能将这个xml传递给Hashtable?

1 个答案:

答案 0 :(得分:0)

使用XMLDecoder

XMLDecoder d = new XMLDecoder(
                   new BufferedInputStream(
                       new FileInputStream("example.xml")));
Hashtable myHashTable = (Hashtable)d.readObject();
d.close();