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

StringgetXPath(Node n)
Constructs a XPath query to the supplied node.
if (null == n)
    return null;
Node parent = null;
Stack hierarchy = new Stack();
StringBuffer buffer = new StringBuffer();
hierarchy.push(n);
parent = n.getParentNode();
while (null != parent && parent.getNodeType() != Node.DOCUMENT_NODE) {
...
StringgetXPath(Node n)
Constructs a XPath query to the supplied node.
if (null == n)
    return null;
Node parent = null;
Stack<Node> hierarchy = new Stack<Node>();
StringBuffer buffer = new StringBuffer();
hierarchy.push(n);
parent = n.getParentNode();
while (null != parent && parent.getNodeType() != Node.DOCUMENT_NODE) {
...
StringgetXPath(Node n)
Creates XPath expression for a given DOM node
Preconditions.checkNotNull(n);
Stack<Node> hierarchy = new Stack<Node>();
StringBuilder buffer = new StringBuilder();
hierarchy.push(n);
Node parent = getParent(n);
while (parent != null) {
    hierarchy.push(parent);
    if (parent.getNodeType() == Node.DOCUMENT_NODE) {
...
StringgetXPath(Node n)
Constructs a XPath query to the supplied node.
if (null == n) {
    throw new IllegalArgumentException("Invalid node");
ArrayList<Node> hierarchy = new ArrayList<Node>();
StringBuffer buffer = new StringBuffer();
Node parent = null;
hierarchy.add(n);
parent = n.getParentNode();
...
StringgetXPath(Node node)
get X Path
Node parent = node.getParentNode();
if (parent == null) {
    return "";
return getXPath(parent) + "/" + node.getLocalName();
StringgetXPath(Node node)
get X Path
if (null == node)
    return null;
Node parent = null;
Stack<Node> hierarchy = new Stack<Node>();
StringBuilder buffer = new StringBuilder();
hierarchy.push(node);
parent = node.getParentNode();
while (null != parent && parent.getNodeType() != Node.DOCUMENT_NODE) {
...
StringgetXPath(Node node)
Create a XPath expression denoting the node's absolute position.
StringBuilder sb = new StringBuilder();
buildXPath(node, sb);
return sb.toString();
StringgetXPath(Node node, String xpath)
get X Path
String elementName = "";
if (node instanceof Element) {
    elementName = node.getNodeName();
    int prev_siblings = 1;
    Node prev_sibling = node.getPreviousSibling();
    while (null != prev_sibling) {
        if (prev_sibling.getNodeType() == node.getNodeType()) {
            if (prev_sibling.getNodeName().equalsIgnoreCase(node.getNodeName())) {
...
XPathExpressiongetXPath(String exp, NamespaceContext ctx)
get X Path
if (ctx == null) {
    return getXPath(exp);
XPath mRetVal = sFactory.newXPath();
mRetVal.setNamespaceContext(ctx);
return mRetVal.compile(exp);
XPathExpressiongetXPath(String expression)
get X Path
try {
    return getXPathFactory().newXPath().compile(expression);
} catch (XPathExpressionException e) {
    throw new RuntimeException("Invalid XPath Expression " + expression, e);