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

public class BilXml {
    public static void main(String[] args) {
        readXml("C:\\bil.xml");
    }

    private static void readXml(String filename) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new File(filename));
            NodeList biler = doc.getElementsByTagName("Bil");
            for (int i = 0; i < biler.getLength(); i++) {
                NodeList bilinfo = biler.item(i).getChildNodes();
                for (int j = 0; j < bilinfo.getLength(); j++) {
                    if (bilinfo.item(j).getNodeType() == Node.ELEMENT_NODE  && bilinfo.item(j).getNodeName().equals("type")) {
                        System.out.println("type=" + bilinfo.item(j).getFirstChild().getNodeValue());
                    }
                    if (bilinfo.item(j).getNodeType() == Node.ELEMENT_NODE  && bilinfo.item(j).getNodeName().equals("ejer")) {
                        System.out.println("ejer=" + bilinfo.item(j).getFirstChild().getNodeValue());
                    }
                    if (bilinfo.item(j).getNodeType() == Node.ELEMENT_NODE  && bilinfo.item(j).getNodeName().equals("specs")) {
                        System.out.println("hp=" + bilinfo.item(j).getAttributes().getNamedItem("hp").getNodeValue());
                        System.out.println("topfart=" + bilinfo.item(j).getAttributes().getNamedItem("topfart").getNodeValue());
                    }
                }
            }
        } catch (FactoryConfigurationError e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return;
    }
}
