Java XPath Evaluate evalXPath(Node node, String xPath)

Here you can find the source of evalXPath(Node node, String xPath)

Description

eval X Path

License

BSD License

Declaration

public static List<Node> evalXPath(Node node, String xPath) 

Method Source Code

//package com.java2s;
//License from project: BSD License 

import java.util.ArrayList;

import java.util.List;

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 {
    private static XPath xpath = XPathFactory.newInstance().newXPath();

    public static List<Node> evalXPath(Node node, String xPath) {
        synchronized (xpath) {
            try {
                NodeList nodes = (NodeList) xpath.evaluate(xPath, node, XPathConstants.NODESET);
                int size = nodes.getLength();
                ArrayList<Node> list = new ArrayList<Node>(size);
                for (int i = 0; i < size; i++)
                    list.add(nodes.item(i));
                return list;
            } catch (XPathExpressionException e) {
                throw new RuntimeException("Invalid XPath expression:" + xPath);
            }//from  w w w  . j a v a  2s .c o m
            // InputSource inputSource = new InputSource("myXMLDocument.xml");
        }
    }
}

Related

  1. evaluateXPathExpression(final String expression, final Node node)
  2. evaluateXPathExpressionAndReturnNode(String expression, Node node)
  3. evaluateXPathExpressionAndReturnNodeList(String expression, Node node)
  4. evaluateXPathQuery(Document doc, NamespaceContext context, String xPathQuery)
  5. evalXPath(Node d, String expr, QName returnType)
  6. evalXPath(String expression, Object item, Class type)
  7. evalXPath(String path, Document doc)
  8. evalXpath(String xpath, Object item)
  9. evalXPathAsString(Object item, String xpath, XPathFactory factory)