Question

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?

Was it helpful?

Solution

Use the XMLDecoder.

XMLDecoder d = new XMLDecoder(
                   new BufferedInputStream(
                       new FileInputStream("example.xml")));
Hashtable myHashTable = (Hashtable)d.readObject();
d.close();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top