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

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

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

import org.apache.xpath.XPathAPI;

public class TagIndex {
    public static void main(String[] args) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse("C:\\test.xml");
            Element res2 = (Element)XPathAPI.selectSingleNode(doc.getDocumentElement(), "/medlemmer/medlem[2]");
            Element name2 = (Element)res2.getElementsByTagName("navn").item(0);
            System.out.println(name2.getFirstChild().getNodeValue());
            Element res1 = (Element)XPathAPI.selectSingleNode(doc.getDocumentElement(), "/medlemmer/medlem[1]");
            Element name1 = (Element)res1.getElementsByTagName("navn").item(0);
            System.out.println(name1.getFirstChild().getNodeValue());
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }
}
