DOM4J:检索按属性值过滤的节点的值 [英] DOM4J: retrieve value of a node filtering by attribute value
本文介绍了DOM4J:检索按属性值过滤的节点的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个给定的xml,其结构如下:
I have a given xml structured like this:
<elem>
<val id="1">aaa</val>
<val id="2">bbb</val>
</elem>
使用SAXReader(DOM4J),如何获取ID = 1(示例中为'aaa')的节点中包含的值?
With SAXReader (DOM4J), how can I get the value contained into the node with id = 1 ('aaa' in the example)?
我已经尝试过了:
String value = elem.element("val[@id='1']")
其中elem是正确的"path.current",但是它没有用.
where elem is the right "path.current", but it didn't work.
可能我用错误的语法写了条件..建议?
Probably I'm writing the condition with a wrong syntax.. suggestions?
推荐答案
xpath语法看起来不错,但是您应该改用selectSingleNode方法.
The xpath syntax looks fine, but you should use the selectSingleNode method instead.
Node value = elem.selectSingleNode("val[@id='1']/text()");
这篇关于DOM4J:检索按属性值过滤的节点的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文