2012-05-09 40 views
0

有没有人知道如何在使用dom4j元素时自动获取带密钥的xpath?使用dom4j创建带密钥的xpath

让我解释一下:

考虑下面的XML我创建了一个dom4j的文档。

<response> 
    <installation> 
    <id>0001</cid> 
    <code>0000</code> 
    </installation> 
    <installation> 
    <id>0002</cid> 
    <code>0078</code> 
    </installation> 
</response> 

通过文档循环我可以创建使用element.getUniquePath()element.getPath()

/response/installation[1]/code 
or 
/response/installation/code 

到目前为止好每一个元素的XPath,但足以不太好。由于对象的顺序无法预先知道,所以我想在我的xpath中使用键。

/response/installation[id=0001]/code 

现在我的问题是,如果有人知道如何做到这一点。

在此先感谢。

回答

1

我不知道有什么方法可以改变getUniquePathgetPath背后的策略。

如果遍历代码中的元素,则可以检索前面的兄弟节点,并通过检索id标记之前的文本内容之一来生成一个XPath。

否则,此XPath将起作用:/response/installation/code[preceding-sibling::id[text()=0002]]