Java XPath Create xpathNodes(Node node, String expression)

Here you can find the source of xpathNodes(Node node, String expression)

Description

xpath Nodes

License

Open Source License

Declaration

public static NodeList xpathNodes(Node node, String expression) throws XPathExpressionException 

Method Source Code


//package com.java2s;
/*-****************************************************************************** 
 * Copyright (c) 2014 Iwao AVE!./*from   w  ww  . j  a  v  a2  s  .co  m*/
 * 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;
import org.w3c.dom.NodeList;

public class Main {
    public static NodeList xpathNodes(Node node, String expression) throws XPathExpressionException {
        return xpathNodes(node, expression, null);
    }

    public static NodeList xpathNodes(Node node, String expression, NamespaceContext nsContext)
            throws XPathExpressionException {
        return (NodeList) evaluateXpath(expression, node, XPathConstants.NODESET, nsContext);
    }

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

Related

  1. xpath()
  2. xpath(String expression)
  3. xpath(String xml, String xpath)
  4. xPath(String xPath)
  5. xpathNode(Node node, String xpath)
  6. xPathNodes(String expr, Object context)
  7. xPathSearch(String input, String expression)