简体   繁体   中英

How to read an xml and pass into a Hashtable?

I'm programming in Java and I have this code to pass a Hashtable to an xml file.

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

And then my xml is something like this:

<?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>

My question is: what I have to do to pass this xml into the Hashtable another time?

Use the XMLDecoder .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM