Example usage for javax.xml.xpath XPathExpression evaluate

List of usage examples for javax.xml.xpath XPathExpression evaluate

Introduction

In this page you can find the example usage for javax.xml.xpath XPathExpression evaluate.

Prototype

public Object evaluate(InputSource source, QName returnType) throws XPathExpressionException;

Source Link

Document

Evaluate the compiled XPath expression in the context of the specified InputSource and return the result as the specified type.

Usage

From source file:Main.java

/**
 * Uses the passed XPath expression to locate a set of nodes in the passed element.
 * @param targetNode The node to search.
 * @param expression The XPath expression.
 * @return A list of located nodes./*from  w w  w  .  j  a  va2s.  c o  m*/
 */
public static List<Node> xGetNodes(Node targetNode, String expression) {
    List<Node> nodes = new ArrayList<Node>();
    XPath xpath = null;
    try {
        xpath = XPathFactory.newInstance().newXPath();
        XPathExpression xpathExpression = xpath.compile(expression);
        NodeList nodeList = (NodeList) xpathExpression.evaluate(targetNode, NODESET);
        if (nodeList != null) {
            for (int i = 0; i < nodeList.getLength(); i++) {
                nodes.add(nodeList.item(i));
            }
        }
        return nodes;
    } catch (Exception e) {
        throw new RuntimeException("XPath:Failed to locate the nodes:" + expression, e);
    }
}

From source file:Main.java

public static NodeList getComponentsWithAttribute(Node xmlDocument, String attribute)
        throws XPathExpressionException {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr = null;
    NodeList n1 = null;/*www  .  j  av a2 s . co  m*/
    expr = xpath.compile("//*[@" + attribute + "]");
    n1 = (NodeList) expr.evaluate(xmlDocument, XPathConstants.NODESET);
    return n1;

}

From source file:Main.java

static public NodeList getNodeList(Document doc, XPath xPath, String pattern) throws XPathExpressionException {
    XPathExpression expr;
    NodeList nl;/*w  w w. j  a v  a 2 s  .  co  m*/

    expr = xPath.compile(pattern);
    nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

    return nl;
}

From source file:Main.java

/**
 * Return a list of elements based on xPath
 *///from   www. j  a va2 s  .  c o  m
static NodeList getElementsByAttribute(Document doc, String xPath) {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr = null;
    NodeList nl = null;
    try {
        expr = xpath.compile(xPath);
        nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return nl;
}

From source file:Main.java

/**
 * Return one element based on xPath//from   w w  w. ja  va2  s.c om
 */
static Element getElementByAttribute(Document doc, String xPath) {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr = null;
    NodeList nl = null;
    try {
        expr = xpath.compile(xPath);
        nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return (Element) nl.item(0);
}

From source file:Main.java

/**
 * Gets the node list from the given xml file and the given xpath expression.
 * /*w w  w . j  a v a  2 s  .co m*/
 * @param xml
 *            the xml
 * @param xpathExpression
 *            the xpath expression
 * @return the node list
 * @throws XPathExpressionException
 *             the x path expression exception
 * @throws ParserConfigurationException
 *             the parser configuration exception
 * @throws SAXException
 *             the sAX exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static NodeList getNodeList(File xml, String xpathExpression)
        throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse(xml);
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile(xpathExpression);

    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    return nodes;
}

From source file:Main.java

/**
 * Gets the node list from the given xml file and the given xpath expression.
 * //from   w ww .j  av a 2  s.  c  o m
 * @param xml
 *            the xml file as string.
 * @param xpathExpression
 *            the xpath expression as string.
 * @return the node list
 * @throws XPathExpressionException
 *             the x path expression exception
 * @throws ParserConfigurationException
 *             the parser configuration exception
 * @throws SAXException
 *             the sAX exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static NodeList getNodeList(String xml, String xpathExpression)
        throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse(xml);
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile(xpathExpression);

    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    return nodes;
}

From source file:Main.java

/**
 * Return the Element corresponding the the XPath
 * /*from   w ww . j a v a 2  s . c o m*/
 * @param xmlNode
 * @param xpathString
 * @return
 * @throws XPathExpressionException
 */
public static NodeList getNodeList(Node xmlNode, String xpathString) {
    try {
        XPathExpression expr = createXPathExpression(xpathString);
        return (NodeList) expr.evaluate(xmlNode, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static NodeList getNodeList(Node node, String path) {
    try {//w w  w  .j  av a2 s .  co m
        slowGetValue++;
        // System.out.println("slow : " + slowGetValue + " - " + path);

        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xpath = xPathFactory.newXPath();
        XPathExpression expr;
        expr = xpath.compile(path);
        NodeList list = (NodeList) expr.evaluate(node, XPathConstants.NODESET);

        return list;
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static NodeList getXPathNodes(String search, Document domDocument, XPath xpathInstance) {
    try {//from   w w  w. j a v  a  2s  .c o  m
        XPathExpression expr = xpathInstance.compile(search);
        return (NodeList) expr.evaluate(domDocument, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
        return null;
    }
}