import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; public class UseXPath{ public static void main(String args[]){ XPath xpath = XPathFactory.newInstance().newXPath(); InputSource inputSource = new InputSource("test.xml"); String expression = "//test/unko[@id='hello']"; try { NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); String str = node.getTextContent(); System.out.println(str); } } catch (XPathExpressionException e) { e.printStackTrace(); } } }