Java tutorial
//package com.java2s; import org.w3c.dom.Node; public class Main { public static String path(Node n) { if (n == null) return ""; switch (n.getNodeType()) { case Node.ATTRIBUTE_NODE: { return path(n.getParentNode()) + "/@" + n.getNodeName(); } case Node.TEXT_NODE: { return path(n.getParentNode()) + "/text()"; } case Node.CDATA_SECTION_NODE: { return path(n.getParentNode()) + "/cdata()"; } case Node.ELEMENT_NODE: { return path(n.getParentNode()) + "/" + n.getNodeName(); } case Node.DOCUMENT_NODE: { return ""; } default: { return ""; } } } }