Here you can find the source of getXPath(Node node, String xpath)
private static String getXPath(Node node, String xpath)
//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); } }