// 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.*;

import javax.xml.parsers.*;
import org.w3c.dom.*;

public class TestMyDom {
   public static void main(String[] args) throws Exception {
      // create DOM parser
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      dbf.setValidating(false);
      dbf.setIgnoringComments(false);
      dbf.setIgnoringElementContentWhitespace(true);
      dbf.setCoalescing(false);
      dbf.setExpandEntityReferences(true);
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.parse(new File("medarbejdere_noDTD.xml"));

      // recurse through whole tree
      recurse(doc.getDocumentElement());

      return;
   }

   private static void recurse(Node parent) throws Exception {
      switch(parent.getNodeType()) {
         case Node.ELEMENT_NODE:
            System.out.println("element: " + parent.getNodeName());
            NamedNodeMap atts = parent.getAttributes();
            for(int i = 0; i < atts.getLength(); i++) {
               System.out.println("attribute: " + atts.item(i).getNodeName() + "=\"" + atts.item(i).getNodeValue() + "\"");
            }
            Node child = parent.getFirstChild();
            while(child != null) {
               recurse(child);
               child = child.getNextSibling();
            }
            break;
         case Node.TEXT_NODE:
            if(!parent.getNodeValue().trim().equals("")) {
               System.out.println("text: #" + parent.getNodeValue() + "#");
            }
            break;
         default:
            // nothing
      }
      return;
   }
}
