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

/**
 * Applies the specified XPath expression to the given node and returns the
 * first result.//from w  w  w  .ja v a 2s  .  c o m
 * 
 * @param node the root element; the given XPath expression is applied to
 *        this node
 * @param path the XPath expression
 * @return the first node corresponding to the specified XPath expression
 * @throws XPathExpressionException
 */
public static Node evalFirst(Node node, String path) throws XPathExpressionException {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile(path);
    final Node result = (Node) expr.evaluate(node, XPathConstants.NODE);
    return result;
}

From source file:Main.java

public static Object executeXPath(Document dom, String xpath, QName returnType)
        throws XPathExpressionException {
    XPathExpression expr = XPathFactory.newInstance().newXPath().compile(xpath);
    return expr.evaluate(dom, returnType);
}

From source file:Main.java

private static String getEmployeeNameById(Document doc, XPath xpath, int id) throws Exception {
    String name = null;// ww w  . jav a2  s .co  m
    XPathExpression expr = xpath.compile("/Employees/Employee[@id='" + id + "']/name/text()");
    name = (String) expr.evaluate(doc, XPathConstants.STRING);
    return name;
}

From source file:Main.java

public static NodeList evaluateXPathQuery(Document doc, NamespaceContext context, String xPathQuery)
        throws XPathExpressionException {
    XPathExpression expr = createXPathExpression(context, xPathQuery);
    return (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
}

From source file:Main.java

public static NodeList executePath(final Node node, final XPathExpression expression)
        throws XPathExpressionException {
    return (NodeList) expression.evaluate(node, XPathConstants.NODESET);
}

From source file:Main.java

public static NodeList getValueAsNodes(String filePath, String value)
        throws ParserConfigurationException, SAXException, IOException {
    NodeList nodeList = null;/*from   w  ww .j a  va  2  s . co  m*/
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    try {
        XPathExpression expr = xpath.compile(value);
        nodeList = (NodeList) expr.evaluate(getDoc(filePath), XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return nodeList;
}

From source file:Main.java

public static NodeList SelectNodes(Document doc, String xmlpath) {
    NodeList nodes = null;//from w w w .  j av  a 2 s  . c o  m
    Object result;
    try {
        XPathFactory xFactory = XPathFactory.newInstance();
        XPath xpath = xFactory.newXPath();

        XPathExpression expr;
        expr = xpath.compile(xmlpath);
        result = expr.evaluate(doc, XPathConstants.NODESET);
        nodes = (NodeList) result;
    } catch (XPathExpressionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return nodes;
}

From source file:Main.java

public static Node getChildNode(Document doc, String parentTag, String childnode) {
    try {/*from  w  w w.j  ava  2 s . com*/
        XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xpath.compile("//" + parentTag + "/" + childnode);
        Object obj = expr.evaluate(doc, XPathConstants.NODE);
        return obj != null ? (Node) obj : null;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Evaluates the XPath expression in the specified context and returns whether such element was found.
 *
 * @param node       the XML document to evaluate
 * @param expression the compiled XPath expression
 * @return <code>true</code> if the given expression evaluates to an existing element in the given node,
 * <code>false</code> otherwise
 *///from   ww  w  . j  a v a2s  .  co  m
public static boolean evaluate(Node node, XPathExpression expression) {
    try {
        Boolean result = (Boolean) expression.evaluate(node, XPathConstants.BOOLEAN);
        return result != null && result;
    } catch (XPathExpressionException e) {
        return false;
    }
}

From source file:Main.java

public static Object getXMLValue(String xml, String xQuery, QName resultType)
        throws XPathExpressionException, IOException, SAXException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document doc = db.parse(new InputSource(new StringReader(xml)));

    XPathFactory xPathfactory = XPathFactory.newInstance();

    XPath xPath = xPathfactory.newXPath();
    XPathExpression xPathExpression = xPath.compile(xQuery);
    return xPathExpression.evaluate(doc, resultType);
}