How can I get the corresponding XPath Query String from a selected TreePath?
a
|-b
+-c
|-b
+-d
If I select "d" I want to get something like /a/b[2]/d
EDIT: For now I wanted to loop through the tree.getSelectionPath().toString().split(",") but the information you will get is /a/b/d - you can not figure out that b should be b[2]
Finally I got it - maybe someone else is interested in a solution
DefaultMutableTreeNode selected = (DefaultMutableTreeNode) tree.getSelectionPath().getLastPathComponent();
String xpath = "";
while (selected.getParent() != null) {
int index = 1;
String tag = selected.toString();
DefaultMutableTreeNode selected2 = selected;
while ((selected2 = selected2.getPreviousSibling()) != null) {
if (tag.equals(selected2.toString())) index++;
}
xpath = "/" + tag + "[" + index + "]" + xpath;
if (selected.getParent() == null) {
selected = null;
} else {
selected = (DefaultMutableTreeNode) selected.getParent();
}
}
LOG.info(xpath);