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 NodeList getChildNodesByExpression(Document doc, String expression) {
    try {//from  w ww . j av a2  s  . com
        XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xpath.compile(expression);

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

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Get node from XML document using xpath expression
 * /*ww  w .  j  a va 2s  .co  m*/
 * @param doc
 * @param expression
 * @return
 * @throws XPathExpressionException
 */
public static Node getNode(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 (Node) result;
}

From source file:Main.java

/**
 * Get a string from an XPath expression.
 * //from  www. j  av  a2  s .  c o m
 * @param node node
 * @param expr XPath expression
 * @return string
 * @throws XPathExpressionException 
 */
public static String getString(Node node, XPathExpression expr) throws XPathExpressionException {
    return (String) expr.evaluate(node, XPathConstants.STRING);
}

From source file:Main.java

private static NodeList findElements(String xpathExpression, Document doc, XPath xpath) throws Exception {
    NodeList nodes = null;//w  ww .jav  a 2s  .  c o m
    if (doc != null) {
        XPathExpression expr = xpath.compile(xpathExpression);
        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        nodes = (NodeList) result;
    }
    return nodes;
}

From source file:Main.java

/**
 * Get nodelist from XML document using xpath expression
 * //  w  w  w .  j  av a 2 s  .  c o  m
 * @param doc
 * @param expression
 * @return
 * @throws XPathExpressionException
 */
public static NodeList getNodeList(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.NODESET);
    return (NodeList) result;
}

From source file:Main.java

public static Object execXpathGetNodeList(String srcXmlString, String xPath) {
    Object result = null;//from   w  ww .  j a v a2s  .co m
    try {
        Document doc = stringToDoc(srcXmlString);
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile(xPath);
        result = expr.evaluate(doc, XPathConstants.NODESET);
    } catch (Exception ex) {
        logger.error(ex);
    }
    return result;
}

From source file:Main.java

/**
 * /*from   w w  w.  j a  va  2 s . co m*/
 * @param xPathS
 * @param node
 * @param nsuri
 * @param pre
 * @param returnType
 * @return Return type is one of XPathConstants .BOOLEAN, .NODE, .NODESET,
 *         .NUMBER, .STRING
 * @throws Exception
 */
public static Object getNodesListXpath(final String xPathS, final Node node, final String nsuri,
        final String pre, final QName returnType) throws Exception {
    Object matches = null;
    System.setProperty("javax.xml.xpath.XPathFactory:" + XPathConstants.DOM_OBJECT_MODEL, XPATH_FACTORY);

    XPathFactory xpathFactory = XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL);
    XPath xpath = xpathFactory.newXPath();
    XPathExpression xpe = xpath.compile(xPathS);
    matches = xpe.evaluate(node, returnType);

    return matches;
}

From source file:Main.java

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

From source file:Main.java

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

From source file:Main.java

private static Node getNodeById(Document doc, String id) throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile("//" + MESSAGE + "[@id='" + id + "']");
    return (Node) expr.evaluate(doc, XPathConstants.NODE);
}