Example usage for javax.xml.xpath XPathConstants BOOLEAN

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

Introduction

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

Prototype

QName BOOLEAN

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

Click Source Link

Document

The XPath 1.0 boolean data type.

Maps to Java Boolean .

Usage

From source file:ApplyXPathJAXP.java

public static void main(String[] args) {
    QName returnType = null;/*from w  w w  .  ja  va2  s  .  co m*/

    if (args.length != 3) {
        System.err.println("Usage: java ApplyXPathAPI xml_file xpath_expression type");
    }

    InputSource xml = new InputSource(args[0]);
    String expr = args[1];

    // set the return type
    if (args[2].equals("num"))
        returnType = XPathConstants.NUMBER;
    else if (args[2].equals("bool"))
        returnType = XPathConstants.BOOLEAN;
    else if (args[2].equals("str"))
        returnType = XPathConstants.STRING;
    else if (args[2].equals("node"))
        returnType = XPathConstants.NODE;
    else if (args[2].equals("nodeset"))
        returnType = XPathConstants.NODESET;
    else
        System.err.println("Invalid return type: " + args[2]);

    // Create a new XPath
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();

    Object result = null;
    try {
        // compile the XPath expression
        XPathExpression xpathExpr = xpath.compile(expr);

        // Evaluate the XPath expression against the input document
        result = xpathExpr.evaluate(xml, returnType);

        // Print the result to System.out.
        printResult(result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static Boolean selectBoolean(Document xmlDocument, String expression) throws XPathExpressionException {
    return (Boolean) selectObject(xmlDocument, expression, XPathConstants.BOOLEAN);
}

From source file:Main.java

public static boolean evaluateXPathBool(Node InNode, String xpath) throws Exception {
    Boolean B = (Boolean) getNodesListXpath(xpath, InNode, "", "", XPathConstants.BOOLEAN);
    return B.booleanValue();
}

From source file:Main.java

public static boolean evaluateXPathBool(final Node inNode, final String xpath) throws Exception {
    Boolean xPathBool = (Boolean) getNodesListXpath(xpath, inNode, "", "", XPathConstants.BOOLEAN);
    return xPathBool.booleanValue();
}

From source file:Main.java

/**
 * Evaluates a boolean xpath. Expensive as it creates a new xpath for every evaluation.
 *//*from   w w w  .j a v a2s.  c om*/
public static boolean evaluateBoolean(String xpathExpression, Document doc) throws XPathExpressionException {
    javax.xml.xpath.XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    Boolean result = (Boolean) xpath.evaluate(xpathExpression, doc, XPathConstants.BOOLEAN);
    return result;
}

From source file:Main.java

public static boolean xpathBool(Node node, String expression) throws XPathExpressionException {
    return ((Boolean) evaluateXpath(expression, node, XPathConstants.BOOLEAN, null)).booleanValue();
}

From source file:Main.java

public static boolean getBooleanPath(String path, Element localElement, Document doc)
        throws XPathExpressionException {
    // Note: if using absolute path, then the root element must also be specified,
    // that is, it should be like "/clinical_studies/clinical_study/..."
    XPath xpath = factory.newXPath();
    Object element = path.startsWith("/") || localElement == null ? doc : localElement;
    boolean res = (Boolean) xpath.evaluate(path, element, XPathConstants.BOOLEAN);
    return res;/*w  w  w  .  jav a 2s .c om*/
}

From source file:Main.java

/**
 * Convenience method that performs an xpath evaluation to determine whether the expression
 * evaluates to true (a node exists)./* w  ww. j  a  va2  s  .c o  m*/
 * This is method exists only to disambiguate the cases of determining the *presence* of a node
 * and determining the *boolean value of the node as converted from a string*, as the syntaxes
 * are very similar and could be misleading.
 *
 * @param xpath      the XPath object
 * @param expression the XPath expression
 * @param object     the object on which to evaluate the expression as required by the XPath API, typically a Node
 * @return whether the result of the expression evaluation, which is whether or not a node was present
 * @throws XPathExpressionException if the expression fails
 */
public static boolean pathExists(XPath xpath, String expression, Object object)
        throws XPathExpressionException {
    return ((Boolean) xpath.evaluate(expression, object, XPathConstants.BOOLEAN)).booleanValue();
}

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.  jav a2s .  c om*/
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:com.thoughtworks.go.util.XpathUtils.java

public static boolean nodeExists(InputSource inputSource, String xpath) throws XPathExpressionException {
    XPathFactory factory = XPathFactory.newInstance();
    XPathExpression expression = factory.newXPath().compile(xpath);
    Boolean b = (Boolean) expression.evaluate(inputSource, XPathConstants.BOOLEAN);
    return b != null && b;
}