// 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.util.Map;
import java.util.HashMap;
import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Enigma {
   public static void process(Map m) {
      System.out.println(m);
   }
   public static void main(String[] args) {
      try {
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         DocumentBuilder db = dbf.newDocumentBuilder();
         Document doc = db.parse(new File("C:\\enigma.xml"));
         NodeList elements = doc.getElementsByTagName("rotor");
         for (int i = 0; i < elements.getLength(); i++) {
            Node element = (Element) elements.item(i);
            Map m = new HashMap();
            NodeList elements2 = element.getChildNodes();
            for(int j = 0; j < elements2.getLength(); j++) {
               if(elements2.item(j).getNodeType() == Node.ELEMENT_NODE) {
                  Node element2 = (Element) elements2.item(j);
                  m.put(element2.getNodeName(), element2.getFirstChild().getNodeValue());
               }
            }
            process(m);
         }
      } catch (FactoryConfigurationError e) {
         e.printStackTrace();
      } catch (ParserConfigurationException e) {
         e.printStackTrace();
      } catch (SAXException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
      return;
   }
}
