// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Iterator;

public class HashMap2XML {
   private static char Q = '"';
   private static String XML = "<?xml version=" + Q + "1.0" + Q + " encoding=" + Q + "iso-8859-1" + Q + " standalone=" + Q + "yes" + Q + "?>";
   public static void print(HashMap hm, PrintStream ps) throws Exception {
      ps.println(XML);
      ps.println("<hashmap>");
      Iterator it = hm.keySet().iterator();
      while(it.hasNext()) {
         Object key = it.next();
         Object val = hm.get(key);
         String keyval = key.toString();
         String valtyp = val.getClass().getName();
         String valval = val.toString();
         ps.println("<attribute name=" + Q + keyval + Q + " type=" + Q + valtyp + Q + ">" + valval + "</attribute>");
      }
      ps.println("</hashmap>");
      return;
   }
   public static void save(HashMap hm, String fnm) throws Exception {
      print(hm, new PrintStream(new FileOutputStream(fnm)));
      return;
   }
}
