Java XPath Evaluate evaluateXPathBool(final Node inNode, final String xpath)

Here you can find the source of evaluateXPathBool(final Node inNode, final String xpath)

Description

evaluate X Path Bool

License

Apache License

Declaration

public static boolean evaluateXPathBool(final Node inNode, final String xpath) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import javax.xml.namespace.QName;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Node;

public class Main {
    public static final String XPATH_FACTORY = "net.sf.saxon.xpath.XPathFactoryImpl";

    public static boolean evaluateXPathBool(final Node inNode, final String xpath) throws Exception {
        Boolean xPathBool = (Boolean) getNodesListXpath(xpath, inNode, "", "", XPathConstants.BOOLEAN);
        return xPathBool.booleanValue();
    }//from  www.  j a  va  2  s. c o 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;
    }
}

Related

  1. evaluateXpath(String expression, Document document, QName dataType)
  2. evaluateXpath(String expression, Object node, QName returnType, NamespaceContext nsContext)
  3. evaluateXPath(String path, Node e, QName type)
  4. evaluateXPath(String xpath, Object item, QName returnType)
  5. evaluateXPathAsNodeList(String expression, Document document)
  6. evaluateXPathExpr(final File xmlFile, final XPathExpression xPathExpression, final QName returnType)
  7. evaluateXPathExpr(XPathExpression xpath, Node node)
  8. evaluateXPathExpression(final String expression, final Node node)
  9. evaluateXPathExpressionAndReturnNode(String expression, Node node)