Java Utililty Methods XPath Create

List of utility methods to do XPath Create

Description

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

Method

StringgetXPathExpression(final Node node)
Returns the unique XPath expression for the given node.
String xpathExpression = node.getNodeName();
if (node.getParentNode() != null) {
    int index = 0;
    Node prec = node;
    while (prec != null) {
        if (prec.getNodeName().equals(node.getNodeName())) {
            index++;
        prec = prec.getPreviousSibling();
    if (node.getParentNode() instanceof Document) {
        ; 
    } else {
        xpathExpression = getXPathExpression(node.getParentNode()) + "/" + xpathExpression 
                + "[" + String.valueOf(index) + "]"; 
return xpathExpression;
XPathExpressiongetXPathExpression(String expression)
get X Path Expression
return getXPath().compile(expression);
XPathExpressiongetXPathExpression(String path)
get X Path Expression
path = path.trim();
try {
    XPath xp = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xp.compile(path);
    return expr;
} catch (Exception ex) {
    throw new RuntimeException(ex);
XPathExpressiongetXPathExpression(String xpath, NamespaceContext namespaceContext)
get X Path Expression
return createXPathExpression(xpath, namespaceContext);
XPathExpressiongetXPathExpression(String xpathStr)
Get xpath expression object by xpath string
return XPathFactory.newInstance().newXPath().compile(xpathStr);
XPathExpressiongetXPathExpression(String xPathString)
get X Path Expression
SoftReference<XPathExpression> ref = xPathExpressionsCache.get(xPathString);
XPathExpression expression = ref == null ? null : ref.get();
if (expression == null)
    xPathExpressionsCache.put(xPathString,
            new SoftReference<XPathExpression>(expression = getSharedXPath().compile(xPathString)));
return expression;
NodegetXpathExpressionNode(Object xprContext, String xpExpression)
get Xpath Expression Node
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression xpe = xpath.compile(xpExpression);
Node node = (Node) xpe.evaluate(xprContext, XPathConstants.NODE);
return node;
StringgetXpathExpressionValue(Object xprContext, String xpExpression)
get Xpath Expression Value
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression xpe = xpath.compile(xpExpression);
Node valueNode = (Node) xpe.evaluate(xprContext, XPathConstants.NODE);
String value = null;
if (valueNode != null)
    value = valueNode.getNodeValue();
if (value != null) {
    if (valueNode.getNodeType() == Node.TEXT_NODE) {
...
StringgetXPathExprFromNode(Node node)
Generates an XPath expression that will return only the given node as its result.
short nodeType = getNodeType(node);
switch (nodeType) {
case Node.ELEMENT_NODE:
case Node.TEXT_NODE:
case Node.PROCESSING_INSTRUCTION_NODE:
    return getXPathFromVector(getVectorPathFromNode(node));
case Node.DOCUMENT_NODE:
    return "/";
...
StringgetXPathExprFromNode(Node node)
Generates an XPath expression that will return only the given node as its result.
short nodeType = getNodeType(node);
switch (nodeType) {
case Node.ELEMENT_NODE:
case Node.TEXT_NODE:
case Node.PROCESSING_INSTRUCTION_NODE:
    return getXPathFromVector(getVectorPathFromNode(node));
case Node.DOCUMENT_NODE:
    return "/";
...