Java tutorial
//package com.java2s; import java.io.IOException; import java.io.InputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.SAXException; public class Main { private static DocumentBuilder dombuilder = null; private static XPath xpath = null; public static Node getSingleNode(InputStream is, String xpathStr) throws SAXException, IOException, XPathExpressionException { Node result = null; Document doc = null; doc = dombuilder.parse(is); //"//db/@dbType" result = (Node) xpath.compile(xpathStr).evaluate(doc, XPathConstants.NODE); return result; } public static Node getSingleNode(String fileName, String xpathStr) throws SAXException, IOException, XPathExpressionException { Node result = null; Document doc = null; doc = dombuilder.parse(fileName); //"//db/@dbType" result = (Node) xpath.compile(xpathStr).evaluate(doc, XPathConstants.NODE); return result; } }