Java XPath Create getXPath(Node node, String xpath)

Here you can find the source of getXPath(Node node, String xpath)

Description

get X Path

License

Open Source License

Declaration

private static String getXPath(Node node, String xpath) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    private static String getXPath(Node node, String xpath) {
        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())) {
                        prev_siblings++;
                    }//from  www .  ja v a2  s .c  om
                }
                prev_sibling = prev_sibling.getPreviousSibling();
            }
            elementName = elementName.concat("[" + prev_siblings + "]");
        }
        Node parent = node.getParentNode();
        if (parent == null) {
            return xpath;
        }
        return getXPath(parent, "/" + elementName + xpath);
    }
}

Related

  1. getXPath(Node n)
  2. getXPath(Node n)
  3. getXPath(Node node)
  4. getXPath(Node node)
  5. getXPath(Node node)
  6. getXPath(String exp, NamespaceContext ctx)
  7. getXPath(String expression)
  8. getXPathExpression(final Node node)
  9. getXPathExpression(String expression)