Java tutorial
//package com.java2s; /*-****************************************************************************** * Copyright (c) 2014 Iwao AVE!. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Iwao AVE! - initial API and implementation and/or initial documentation *******************************************************************************/ import javax.xml.namespace.NamespaceContext; import javax.xml.namespace.QName; 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; public class Main { public static Node xpathNode(Node node, String expression) throws XPathExpressionException { return (Node) evaluateXpath(expression, node, XPathConstants.NODE, null); } public static Object evaluateXpath(String expression, Object node, QName returnType, NamespaceContext nsContext) throws XPathExpressionException { XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); if (nsContext != null) { xpath.setNamespaceContext(nsContext); } return xpath.evaluate(expression, node, returnType); // XPathExpression xpathExpr = xpath.compile(expression); // return xpathExpr.evaluate(node, returnType); } }