// 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 javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.xpath.XPathAPI;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XPathFun {
    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse("C:\\fun.xml");
        NodeList many = XPathAPI.selectNodeList(doc.getDocumentElement(), "//rss/channel/item");
        for(int i = 0; i < many.getLength(); i++) {
            Element one = (Element)many.item(i);
            Node title = XPathAPI.selectSingleNode(one, "title/text()");
            Node tag = XPathAPI.selectSingleNode(one, "tag/text()");
            if(tag != null) {
                System.out.println(title.getNodeValue() + " is " + tag.getNodeValue());
            } else {
                System.out.println(title.getNodeValue() + " (no tag)");
            }
        }
    }
}
