// 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.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.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Parse2Dom {
   private final static String XML_FILE = "C:\\test2.xml";
   public static void main(String[] args) {
      readXml(XML_FILE);
   }
   private static void readXml(String filename) {
      try {
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         DocumentBuilder db = dbf.newDocumentBuilder();
         Document doc = db.parse(new File(filename));
         NodeList elements = doc.getElementsByTagName("afdeling");
         for (int i = 0; i < elements.getLength(); i++) {
            Node element = elements.item(i);
            NamedNodeMap attrs = element.getAttributes();
            String id = attrs.getNamedItem("id").getNodeValue();
            String fork = attrs.getNamedItem("fork").getNodeValue();
            System.out.println("INSERT INTO afdeling(id,fork) VALUES(" + id + "," + fork + "')");
            NodeList subelements = element.getChildNodes();
            for(int j = 0; j < subelements.getLength(); j++) {
               if(subelements.item(j).getNodeName().equals("medarbejder")) {
                   NamedNodeMap subattrs = subelements.item(j).getAttributes();
                   String subid = subattrs.getNamedItem("id").getNodeValue();
                   String subnavn = subattrs.getNamedItem("navn").getNodeValue();
                   System.out.println("INSERT INTO medarbejder(afdeling,id,navn) VALUES(" + id + "," + subid + ",'" + subnavn + "')");
                   NodeList subsubelements = subelements.item(j).getChildNodes();
                   for(int k = 0; k < subsubelements.getLength(); k++) {
                      if(subsubelements.item(k).getNodeName().equals("papir")) {
                          NamedNodeMap subsubattrs = subsubelements.item(k).getAttributes();
                          String subsubid = subsubattrs.getNamedItem("id").getNodeValue();
                          System.out.println("INSERT INTO papir(medarbejder,id) VALUES(" + subid + ",'" + subsubid + "')");
                   
                      }
                   }
                   
               }
            }
         }
      } catch (FactoryConfigurationError e) {
         e.printStackTrace();
      } catch (ParserConfigurationException e) {
         e.printStackTrace();
      } catch (SAXException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
      return;
   }
}
