访问现有属性返回null

时间:2015-07-31 09:58:28

标签: java xml dom xpath sax

我编写了下面的表达式来从下面的xml文件中检索信息,但是返回的结果是null并且索引超出范围。

请告诉我如何从此元素中检索数据:

 <ele k="maxspeed:zone" v="60"/>

表达

"//node[@lat='53.0337395'] [@lon='8.8489255']/following-sibling/following-   
sibling::ele[1]/@v"

XML

<?xml version='1.0' encoding='utf-8' ?>
<osm>
<node id="25779111" lat="53.0334062" lon="8.8461545"/>
<node id="25779112" lat="53.0338904" lon="8.846314"/>
<node id="25779119" lat="53.0337395" lon="8.8489255"/>
<tag k="maxspeed" v="30"/>
<tag k="maxspeed:zone" v="yes"/>
<ele k="maxspeed:zone" v="60"/>
<node id="25779114" lat="53.334062" lon="8.841545"/>
<node id="25779117" lat="53.338904" lon="8.84614"/>
<node id="25779110" lat="53.33795" lon="8.489255"/>
<tag k="maxspeed" v="32"/>
<tag k="maxspeed:zone" v="no"/>
</osm>

1 个答案:

答案 0 :(得分:2)

你几乎就在那里 - 你需要将::tag[1]添加到第一个following-sibling,如下所示:

"//node[@lat='53.0337395'] [@lon='8.8489255']/following-sibling::tag[1]/following-sibling::ele[1]/@v"

这会检索Attribute='v="60"'