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

public static int getInt(Object node, XPathExpression expression) throws XPathExpressionException {
    return ((Double) expression.evaluate(node, XPathConstants.NUMBER)).intValue();
}

From source file:Main.java

public static int evalXPathAsInt(Object item, String xpath, XPathFactory factory)
        throws XPathExpressionException {
    XPath path = factory.newXPath();
    XPathExpression expr = path.compile(xpath);
    Number result = (Number) expr.evaluate(item, XPathConstants.NUMBER);
    return result == null ? 0 : result.intValue();
}

From source file:Main.java

/**
 * Get the XML Nodes for the specified XPath.
 * //from w  w w.  j a  v  a  2  s.  c  o  m
 * @param path
 *            - XPath String
 * @param parent
 *            - Parent Node.
 * @return
 * @throws Exception
 */
public static NodeList search(String path, Element parent) throws Exception {
    // XPath Query for showing all nodes value
    XPathExpression expr = xpath.compile(path);
    return (NodeList) expr.evaluate(parent, XPathConstants.NODESET);
}

From source file:Main.java

public static NodeList getNodeList(Object node, XPathExpression expression) throws XPathExpressionException {
    return (NodeList) expression.evaluate(node, XPathConstants.NODESET);
}

From source file:Main.java

public static NodeList getNodeListViaXPath(String xpathExpr, String xml) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(xml);
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr = xpath.compile(xpathExpr);

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

    return nl;/*from  www .  jav  a 2s.  c  o  m*/
}

From source file:Main.java

public static Object xpathFind(Document doc, String xpath, QName returnType) {
    try {/*from   w ww.  ja  v a  2  s.c o m*/
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpathToolkit = xpathFactory.newXPath();
        XPathExpression xpathExpression = xpathToolkit.compile(xpath);
        return xpathExpression.evaluate(doc, returnType);
    } catch (Exception e) {
        throw new RuntimeException("Error finding nodes for <" + xpath + ">", e);
    }
}

From source file:Main.java

public static NodeList getNodes(Object doc, String query) throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile(query);
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    return nodes;
}

From source file:Main.java

public static NodeList getNodes(Document doc, String query) throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile(query);
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    return nodes;
}

From source file:Main.java

/**
 * Get a node from an XPath expression.//from w ww .  j  a  va2 s  .  co  m
 * 
 * @param node node
 * @param expr XPath expression
 * @return node list
 * @throws XPathExpressionException 
 */
public static Node getNode(Node node, XPathExpression expr) throws XPathExpressionException {
    return (Node) expr.evaluate(node, XPathConstants.NODE);
}

From source file:Main.java

/**
 * Get one element from XML document using xpath expression
 * /*  w  ww. ja va 2s. c o m*/
 * @param doc
 * @param expression
 * @return
 * @throws XPathExpressionException
 */
public static Element getElement(final Document doc, final String expression) throws XPathExpressionException {
    if (null == expression || expression.isEmpty() || null == doc) {
        return null;
    }
    log.finer("Executing xpath xpression " + expression);
    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xPath.compile(expression);
    Object result = expr.evaluate(doc, XPathConstants.NODE);
    return (Element) result;
}