Java tutorial
//package com.java2s; import java.util.Vector; import org.w3c.dom.*; public class Main { public static String[] getElementPath(Node node) { Vector vector = new Vector(); for (; node != null; node = node.getParentNode()) if (node.getNodeType() == 1) if (!hasSameNamedSibling(node)) vector.addElement(node.getNodeName()); else vector.addElement(node.getNodeName() + "[" + (getElementIndex(node) + 1) + "]"); int i = vector.size(); String as[] = new String[i]; int j = i - 1; for (int k = 0; k < i;) { as[k] = (String) vector.elementAt(j); k++; j--; } return as; } public static boolean hasSameNamedSibling(Node node) { String s = node.getNodeName(); for (Node node1 = node.getPreviousSibling(); node1 != null; node1 = node1.getPreviousSibling()) if (node1.getNodeName().equals(s) && node1.getNodeType() == node.getNodeType()) return true; for (Node node2 = node.getNextSibling(); node2 != null; node2 = node2.getNextSibling()) if (node2.getNodeName().equals(s) && node2.getNodeType() == node.getNodeType()) return true; return false; } public static int getElementIndex(Node node) { String s = node.getNodeName(); int i = 0; for (Node node1 = node.getPreviousSibling(); node1 != null; node1 = node1.getPreviousSibling()) if (node1.getNodeName().equals(s) && node1.getNodeType() == 1) i++; return i; } }