如何获取标识Java中节点的XPath表达式?
我有一个结构如下的XML文档:如何获取标识Java中节点的XPath表达式?,java,xml,dom,xpath,Java,Xml,Dom,Xpath,我有一个结构如下的XML文档: <root> <a> <bd> <cd> <aa/> <bb/> <aa/> </cd> </bd> </a> <a>
<root>
<a>
<bd>
<cd>
<aa/>
<bb/>
<aa/>
</cd>
</bd>
</a>
<a>
<bd>
<cd>
<aa/>
<bb/>
<aa/>
</cd>
</bd>
</a>
<tt>
<at/>
<bt/>
</tt>
</root>
这应该为每个节点打印出一个唯一的路径,并根据需要添加索引
private void printXPaths(Node node, OutputStream stream, String current, Map<String, int> occurences)
{
if (node.getNodeType() != Node.ELEMENT_NODE) {
return;
}
String nodePath = current + "/" + node.getNodeName();
if(occurences.contains(nodePath)) {
int occurrencesCount = occurences[nodePath];
nodePath += "[" + occurrencesCount + "]";
occurences[nodePath] = occurrencesCount + 1;
} else {
occurences[nodePath] = 1;
}
stream.println(nodePath);
NodeList children = node.getChildNodes();
for(int i=0; i<children.getLength(); i++) {
printXPaths(children.item(i), stream, nodePath, occurences);
}
}
public void printXPaths(Document doc, OutputStream stream) {
printXPaths(doc.getDocumentElement(), stream, "", new HashMap<String, int>());
}
private void printXPaths(节点节点、输出流、字符串当前、映射发生)
{
if(node.getNodeType()!=node.ELEMENT\u node){
返回;
}
字符串nodePath=current+“/”+node.getNodeName();
if(发生率.包含(节点路径)){
int-occurrenceScont=事件[节点路径];
节点路径+=“[”+发生率查询+“]”;
发生[节点路径]=发生频率+1;
}否则{
发生次数[节点路径]=1;
}
stream.println(nodePath);
NodeList childrends=node.getChildNodes();
对于(inti=0;i,除了没有添加索引之外,您的代码似乎可以工作;因此我认为您所要做的就是搜索当前节点在其父-子列表中的位置,并将其添加到xpath中
private static String getXPath(Node test_tempNode) {
if (test_tempNode == null
|| test_tempNode.getNodeType() != Node.ELEMENT_NODE) {
return "";
}
//find index of the test_tempNode node in parent "list"
Node parent = test_tempNode.getParentNode();
NodeList childNodes = parent.getChildNodes();
int index = 0;
int found = 0;
for (int i = 0; i < childNodes.getLength(); i++) {
Node current = childNodes.item(i);
if (current.getNodeName().equals(test_tempNode.getNodeName())) {
if (current == test_tempNode) {
found = index;
}
index++;
}
}
String strIdx = "[" + found + "]";
if(index == 1){
strIdx = "";
}
return getXPath(test_tempNode.getParentNode()) + "/"
+ test_tempNode.getNodeName() + strIdx;
}
私有静态字符串getXPath(节点测试\u tempNode){
if(test_tempNode==null
||test_tempNode.getNodeType()!=Node.ELEMENT_Node){
返回“”;
}
//在父“列表”中查找测试节点的索引
Node parent=test_tempNode.getParentNode();
NodeList childNodes=parent.getChildNodes();
int指数=0;
int=0;
对于(int i=0;i
我无法通过此获取xpath中的索引..在我的场景中不起作用。输出应类似于root/a[1]/bd[1]/aa[2]对于每个重复的节点,索引都应该在那里。感谢对我来说不像XML的标记-大概像
这样的标记应该是
。请编辑问题并将XML修改为格式良好的。但这也会为那些不重复的节点添加索引…我们必须只为那些我们再次感谢您的帮助,我正在尝试修改它,如果您首先得到它,请随时通知我。再次感谢感谢Hanks alot先生…这段代码非常有用。找到的值不应总是大于0
?如果是这样,那么您应该在将其分配给找到
private static String getXPath(Node test_tempNode) {
if (test_tempNode == null
|| test_tempNode.getNodeType() != Node.ELEMENT_NODE) {
return "";
}
//find index of the test_tempNode node in parent "list"
Node parent = test_tempNode.getParentNode();
NodeList childNodes = parent.getChildNodes();
int index = 0;
int found = 0;
for (int i = 0; i < childNodes.getLength(); i++) {
Node current = childNodes.item(i);
if (current.getNodeName().equals(test_tempNode.getNodeName())) {
if (current == test_tempNode) {
found = index;
}
index++;
}
}
String strIdx = "[" + found + "]";
if(index == 1){
strIdx = "";
}
return getXPath(test_tempNode.getParentNode()) + "/"
+ test_tempNode.getNodeName() + strIdx;
}