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 Element querySingle(Element element, String query) {
    try {//from  w  w  w  .ja  v a 2 s. co m
        XPathExpression expr = xpath.newXPath().compile(query);
        NodeList list = (NodeList) expr.evaluate(element, XPathConstants.NODESET);

        if (list.getLength() > 0)
            return (Element) list.item(0);

    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static Node getNoge(String path, Node node) throws RuntimeException {
    path = path.trim();//from w w  w .  j  a v a 2  s  .c o  m
    try {
        XPath xp = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xp.compile(path);

        Node lst = (Node) expr.evaluate(node, XPathConstants.NODE);
        return lst;

    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

public static Object evaluateXPath(final XPathExpression expr, final Object rootNode, final QName returnType) {
    try {/* ww w  .  j ava2s .c o m*/
        return expr.evaluate(rootNode, returnType);
    } catch (XPathExpressionException e) {
        throw new IllegalStateException("Error while evaluating xpath expression " + expr, e);
    }
}

From source file:Main.java

public static Object execXpathGetNode(String srcXmlString, String xPath) {
    Object result = null;//from  w ww. j a v  a  2s  .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.NODE);
    } catch (Exception ex) {
        logger.error(ex);
    }
    return result;
}

From source file:Main.java

public static String editPath(String xmlSource, String xPath, String newValue)
        throws ParserConfigurationException, SAXException, IOException, XPathExpressionException,
        TransformerException {//from w  w  w .  ja v a2  s.c  o  m
    Document xmlDoc = readString(xmlSource);
    XPathExpression expr = evaluateThePath(xPath);
    NodeList nl = (NodeList) expr.evaluate(xmlDoc, XPathConstants.NODESET);
    for (int i = nl.getLength(); i > 0; i--) {
        nl.item(0).setNodeValue(newValue);
    }
    return xmlDocToString(xmlDoc);
}

From source file:Main.java

private static Node getNodeObject(String xpathString, Document doc) throws XPathExpressionException {
    // Create a xptah object
    //logger.debug("Getting the node by using xpath " + xpathString);
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();
    XPathExpression expr = xpath.compile(xpathString);
    Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
    //logger.debug("Returning the Node object from xml file");
    return node;//from  www .  j  a v  a 2s . com
}

From source file:Main.java

/**
 * //from   w ww  .ja v a 2s  . c  o m
 * <B>Purpose:</B> Evaluates an XPath and returns the Node list associated
 * witht the XPath
 * 
 * @param element
 * @param xpathstring
 * @return
 * @throws XPathExpressionException
 */
public static NodeList evaluateXPath(Element element, String xpathstring) throws XPathExpressionException {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile(xpathstring);
    NodeList result = (NodeList) expr.evaluate(element, XPathConstants.NODESET);

    return result;
}

From source file:Main.java

public static Object executeXPath(Document document, String expression, QName outputType) {
    Object result = null;//from  ww w .ja va 2  s  .  c  o m
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr;
    try {
        expr = xpath.compile(expression);
        result = expr.evaluate(document, outputType);
    } catch (XPathExpressionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static Node queryNode(Document document, String xPath) {
    try {// w  w  w  .  j av a  2  s  . c  om
        XPathExpression expr = XPathFactory.newInstance().newXPath().compile(xPath);
        return (Node) expr.evaluate(document, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:Main.java

public static String queryText(Document document, String xPath) {
    try {//from w ww.ja  v a2  s  . c om
        XPathExpression expr = XPathFactory.newInstance().newXPath().compile(xPath);
        return (String) expr.evaluate(document, XPathConstants.STRING);
    } catch (XPathExpressionException e) {
        throw new IllegalArgumentException(e);
    }
}