DOM4J: retrieve value of a node filtering by attribute value

给你一囗甜甜゛ 提交于 2019-12-07 22:54:01

问题


I have a given xml structured like this:

<elem>
     <val id="1">aaa</val>
     <val id="2">bbb</val>
</elem>

With SAXReader (DOM4J), how can I get the value contained into the node with id = 1 ('aaa' in the example)?

I've tried this:

String value = elem.element("val[@id='1']")

where elem is the right "path.current", but it didn't work.

Probably I'm writing the condition with a wrong syntax.. suggestions?


回答1:


The xpath syntax looks fine, but you should use the selectSingleNode method instead.

Node value = elem.selectSingleNode("val[@id='1']/text()");



回答2:


You can try the full XPath: /elem/val[@id='1'] or any parent wildcard //val[@id='1']



来源:https://stackoverflow.com/questions/5390445/dom4j-retrieve-value-of-a-node-filtering-by-attribute-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!