Here you can find the source of findNodeByXpath(org.w3c.dom.Document doc, String xpathEx)
Parameter | Description |
---|---|
doc | a parameter |
xpathEx | a parameter |
Parameter | Description |
---|---|
XPathExpressionException | an exception |
@SuppressWarnings("unchecked") public static <T> T findNodeByXpath(org.w3c.dom.Document doc, String xpathEx) throws XPathExpressionException
//package com.java2s; /*/* ww w . j av a 2 s.c om*/ * Copyright (c) 2013 Cisco Systems, Inc. and others. 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 */ import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; public class Main { /** * */ private static final String USER_KEY_NODE = "node"; /** * @param doc * @param xpathEx * @return user data value on found node * @throws XPathExpressionException */ @SuppressWarnings("unchecked") public static <T> T findNodeByXpath(org.w3c.dom.Document doc, String xpathEx) throws XPathExpressionException { T userNode = null; XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); XPathExpression expr = xpath.compile(xpathEx); org.w3c.dom.Node result = (org.w3c.dom.Node) expr.evaluate(doc, XPathConstants.NODE); if (result != null) { userNode = (T) result.getUserData(USER_KEY_NODE); } return userNode; } }