Java Utililty Methods XPath Expression

List of utility methods to do XPath Expression

Description

The list of methods to do XPath Expression are organized into topic(s).

Method

BooleanasBoolean(String expression, Node node)
Evaluates the specified XPath expression and returns the result as a Boolean.
return asBoolean(expression, node, xpath());
LongasLong(String expression, Node node)
Evaluates the specified XPath expression and returns the result as a Long.
String longString = evaluateAsString(expression, node);
return (isEmptyString(longString)) ? null : Long.valueOf(longString);
XPathExpressioncompile(String expression)
compile
return xPath.compile(expression);
XPathExpressioncompile(String expression)
Returns a compiled XPath expression.
return compile(expression, (NamespaceContext) null);
XPathExpressioncompile(String path)
compile
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
try {
    return xpath.compile(path);
} catch (Exception e) {
    throw new RuntimeException(e);
XPathExpressioncompileXPathExpression(String xPathExpression)
compile X Path Expression
final XPathFactory factory = XPathFactory.newInstance();
final XPath xpath = factory.newXPath();
try {
    return xpath.compile(xPathExpression);
} catch (XPathExpressionException e) {
    throw new RuntimeException("Internal error, invalid XPATH expression " + xPathExpression, e);
StringconstructXPathForElement(Element inElem, String xPathRest)
construct X Path For Element
String xpathPart = "/" + inElem.getNodeName();
if (inElem.hasAttribute("id")) {
    xpathPart = "/" + xpathPart + "[@id = '" + inElem.getAttribute("id") + "']";
} else {
    xpathPart += "[" + countPrecedingSiblingsOfType(inElem) + "]";
    if (inElem.getParentNode() != null) {
        if (inElem.getParentNode().getNodeType() == Node.ELEMENT_NODE) {
            return constructXPathForElement((Element) (inElem.getParentNode()), xpathPart + xPathRest);
...
XPathFactorycreateFactory()
create Factory
return XPathFactory.newInstance();
XPathcreateNewXPath(@Nullable final NamespaceContext aNamespaceContext)
Create a new XPath with the passed namespace context using the default XPathFactory .
return createNewXPath(s_aXPathFactory, (XPathVariableResolver) null, (XPathFunctionResolver) null,
        aNamespaceContext);
ElementcreateQueryResourcePropertiesCallBody(String aXqueryExpression)
Creates the frame for SOAP message body for calling 'QueryResourceProperties' method of a service (getting metadata)
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.newDocument();
String methodName = "QueryResourceProperties";
String parameterName = "QueryExpression";
String nameSpace = "http://docs.oasis-open.org/wsrf/2004/06/wsrf-WS-ResourceProperties-1.2-draft-01.xsd";
Element methodElement = doc.createElementNS(nameSpace, methodName);
Element parameterElement = doc.createElementNS(nameSpace, parameterName);
parameterElement.setAttribute("Dialect", "http://www.w3.org/TR/1999/REC-xpath-19991116");
...