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 String evaluate(InputSource source) throws XPathExpressionException;

Source Link

Document

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

Usage

From source file:Main.java

public static String getResultXpathstring(String expr, InputSource inputSource)
        throws XPathExpressionException {
    XPathExpression xExpr = xPath.compile(expr);
    return xExpr.evaluate(inputSource);
}

From source file:Main.java

public static String getValue(Node node, String exprStr) throws XPathExpressionException {
    XPathExpression expr = xpath.compile(exprStr);
    return expr.evaluate(node);
}

From source file:Main.java

/**
 * Evaluate an XPath against a Document, returning a String.
 * //from   w  w  w .j  a va  2s  .co  m
 * @param doc Document
 * @param xp XPath to evaluate against Document
 * @return String found at path or null
 */
public static String xpathOrNull(Document doc, String xp) {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    try {
        XPathExpression expr = xpath.compile(xp);
        return expr.evaluate(doc);
    } catch (XPathExpressionException e) {
        return null;
    }
}

From source file:Main.java

public static String getValueByPath(Node node, String path) {
    XPathExpression expr = compile(path);
    try {/*w ww .  jav a  2s.co  m*/
        return expr.evaluate(node);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

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

        return ret;

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

From source file:com.thoughtworks.go.util.XpathUtils.java

private static String evaluate(XPathFactory factory, InputSource inputSource, String xpath)
        throws XPathExpressionException {
    XPathExpression expression = factory.newXPath().compile(xpath);
    return expression.evaluate(inputSource).trim();
}

From source file:Main.java

public static String getNodeCount(String nodeName, String xmlString) {
    String response = "";
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    try {//from ww w  . j av a  2s .  co  m
        XPathExpression xPathExpression = xPath.compile("count(//" + nodeName + ")");
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xmlString));
        response = xPathExpression.evaluate(is);

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

    return response;
}

From source file:org.fcrepo.kernel.impl.identifiers.HttpPidMinter.java

/**
 * Extract the desired identifier value from an XML response using XPath
**//*  ww w.j  ava  2 s . c o  m*/
private static String xpath(final String xml, final XPathExpression xpath) throws Exception {
    final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    final Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
    return xpath.evaluate(doc);
}

From source file:com.indoqa.maven.wadldoc.AbstractWadlDocumentationMojo.java

private static String getWadlDocumentName(XPathExpression expression, File wadlFile)
        throws MavenReportException {
    try {/* ww w.  j ava 2s .co  m*/
        return expression.evaluate(new InputSource(new FileInputStream(wadlFile)));
    } catch (Exception e) {
        throw new MavenReportException("Can't evaluate XPath expression on " + wadlFile, e);
    }
}

From source file:org.fcrepo.mint.HttpPidMinter.java

/**
 * Extract the desired identifier value from an XML response using XPath
**//*from  w  w w .j  a  v a  2 s . c o m*/
private static String xpath(final String xml, final XPathExpression xpath)
        throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
    final DocumentBuilder builder = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder();
    final Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
    return xpath.evaluate(doc);
}