Java XML Node Find findNodeByXpath(org.w3c.dom.Document doc, String xpathEx)

Here you can find the source of findNodeByXpath(org.w3c.dom.Document doc, String xpathEx)

Description

find Node By Xpath

License

Open Source License

Parameter

Parameter Description
doc a parameter
xpathEx a parameter

Exception

Parameter Description
XPathExpressionException an exception

Return

user data value on found node

Declaration

@SuppressWarnings("unchecked")
public static <T> T findNodeByXpath(org.w3c.dom.Document doc,
        String xpathEx) throws XPathExpressionException 

Method Source Code

//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;
    }
}

Related

  1. findNode(Node node, String nodeName)
  2. findNode(Node rootNode, String nodeName)
  3. findNodeByAttribute(Document document, String tagName, String attributeName, String attributeValue)
  4. findNodeByName(Document doc, String pathExpression)
  5. findNodeByTagName(Document document, String tagName)
  6. findNodeIndex(Node node)
  7. findNodeIndex(Node node)
  8. findNodeLong(Node node)
  9. findNodesByTagName(Document document, String tagName)