Java XPath Evaluate xpathEvalElements(String expr, Object e)

Here you can find the source of xpathEvalElements(String expr, Object e)

Description

xpath Eval Elements

License

Open Source License

Declaration

public static List<Element> xpathEvalElements(String expr, Object e) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    private final static XPath xpath = XPathFactory.newInstance().newXPath();

    public static List<Element> xpathEvalElements(String expr, Object e) {
        NodeList nl = xpathEvalNodes(expr, e);
        List<Element> res = new ArrayList<Element>();
        for (int i = 0; i < nl.getLength(); i++) {
            res.add((Element) nl.item(i));
        }/*from www  . j  a v  a 2 s  .  c o m*/
        return res;
    }

    public static NodeList xpathEvalNodes(String expr, Object e) {
        return (NodeList) xpathEval(expr, e, XPathConstants.NODESET);
    }

    public static Object xpathEval(String expr, Object e, QName type) {
        try {
            return xpath.evaluate(expr, e, type);
        } catch (Exception exc) {
            throw new RuntimeException("Cannot evaluate xpath expression.", exc);
        }
    }
}

Related

  1. execXPath(org.w3c.dom.Node node, String pattern, QName xPathConstantsType)
  2. findNodeAndGetXPath(String qName, String fileName)
  3. getNodesListXpath(final String xPathS, final Node node, final String nsuri, final String pre, final QName returnType)
  4. parseVariable(Node contextNode, String xPathString)
  5. xPath(final Node aNode, final String anXPath, final QName aQName)
  6. xpathEvalNodes(String expr, Object e)