Example usage for javax.xml.xpath XPathConstants STRING

List of usage examples for javax.xml.xpath XPathConstants STRING

Introduction

In this page you can find the example usage for javax.xml.xpath XPathConstants STRING.

Prototype

QName STRING

To view the source code for javax.xml.xpath XPathConstants STRING.

Click Source Link

Document

The XPath 1.0 string data type.

Maps to Java String .

Usage

From source file:Main.java

public static String getString(Object node, XPathExpression expression) throws XPathExpressionException {
    return (String) expression.evaluate(node, XPathConstants.STRING);
}

From source file:Main.java

static public String string(Node context, String expression, String defaultValue) {
    try {/*from www.j  a v  a  2s. c  o  m*/
        String result = (String) xpath.evaluate(expression, context, XPathConstants.STRING);
        if (result == null || result.length() == 0)
            return defaultValue;
        else
            return result;
    } catch (XPathExpressionException ex) {
        ex.printStackTrace();
        throw new RuntimeException("invalid xpath expresion used");
    }
}

From source file:Main.java

public static String queryText(Document document, String xPath) {
    try {/* www  . ja v  a  2  s  . c  o m*/
        XPathExpression expr = XPathFactory.newInstance().newXPath().compile(xPath);
        return (String) expr.evaluate(document, XPathConstants.STRING);
    } catch (XPathExpressionException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:Main.java

public static String evalXPathAsString(Object item, String xpath, XPathFactory factory)
        throws XPathExpressionException {
    XPath path = factory.newXPath();
    XPathExpression expr = path.compile(xpath);
    return (String) expr.evaluate(item, XPathConstants.STRING);
}

From source file:Main.java

public static String extractValue(String xml, String xpathExpression) {
    String actual;/*www.  j a  v  a2  s . co  m*/
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        documentBuilderFactory.setIgnoringElementContentWhitespace(true);
        DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();

        byte[] bytes = xml.getBytes("UTF-8");
        InputStream inputStream = new ByteArrayInputStream(bytes);
        Document doc = docBuilder.parse(inputStream);
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xpath = xPathFactory.newXPath();

        actual = xpath.evaluate(xpathExpression, doc, XPathConstants.STRING).toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return actual;
}

From source file:Main.java

/**
 * Get a string from an XPath expression.
 * //  ww w.j  a  va2s .  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

public static String findStringByXPath(String xPathString, Object source) throws XPathExpressionException {
    return (String) getXPathExpression(xPathString).evaluate(source, XPathConstants.STRING);
}

From source file:Main.java

public static String getStringByPath(String path, Element localElement, Document doc)
        throws XPathExpressionException {
    // Note the difference between this function and function "getStringsByPath"
    // The path for this function should be like "/clinical_studies/clinical_study/brief_title",
    // which returns ONLY ONE string of the first matched element "brief_title"
    XPath xpath = factory.newXPath();
    Object element = path.startsWith("/") || localElement == null ? doc : localElement;
    return (String) xpath.evaluate(path, element, XPathConstants.STRING);
}

From source file:Main.java

public static String xPathSearch(String input, String expression) throws Exception {
    DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression xPathExpression = xPath.compile(expression);
    Document document = documentBuilder.parse(new ByteArrayInputStream(input.getBytes("UTF-8")));
    String output = (String) xPathExpression.evaluate(document, XPathConstants.STRING);
    return output == null ? "" : output.trim();
}

From source file:Main.java

public static String xpathString(String expr, Document doc) {
    try {//from  w ww  .j  a  v  a2  s  .  c o m
        return (String) xpath.evaluate(expr, doc, XPathConstants.STRING);
    } catch (Exception e) {
        throw new RuntimeException("Cannot evaluate XPath:", e);
    }

}