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

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

public class XPathFun {
    public static void main(String[] args) throws Exception {
        String s = "<html><head><title>XPathFun</title></head><body><table id='1'><tr><td>DATA JEG IKKE VIL HAVE UD</td></tr></table><table id='2'><tr><td>DATA JEG VIL HAVE UD</td></tr></table></body></html>";
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(new StringReader(s)));
        XPath xpath = XPathFactory.newInstance().newXPath();
        Node wanted = (Node)xpath.evaluate("//html/body/table[@id='2']/tr/td/text()", doc.getDocumentElement(), XPathConstants.NODE);
        System.out.println(wanted.getNodeValue());
    }
}
