Java tutorial
//package com.java2s; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; public class Main { /** * Returns a single node that matches the given XPath expression. * * @param doc * Document that contains the nodes. * @param expression * XPath expression to be matched. * @return Returns a single node matching the given expression. */ public static Node selectSingleNode(Document doc, String expression) { try { XPath xpath = XPathFactory.newInstance().newXPath(); return (Node) xpath.evaluate(expression, doc, XPathConstants.NODE); } catch (XPathExpressionException e) { // ignore } return null; } }