对于下面发布的XML,我想得到与lat = 53.0319947对应的id的值。
为实现这一目标,我编写了以下代码。不幸的是我没有得到任何结果。请让我知道如何解决它:
String expr0 = "//node[@lat=53.0319947]/id";
xPath.compile(expr0);
String s = (String) xPath.evaluate(expr0, document, XPathConstants.STRING);
System.out.println(s);
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"/>
<node id="25779121" lat="53.0338904" lon="8.85117"/>
<node id="25779122" lat="53.0335748" lon="8.8511796"/>
<node id="25779124" lat="53.0348595" lon="8.8514167"/>
<node id="25779125" lat="53.0325729" lon="8.8511503"/>
<node id="25779126" lat="53.0319947" lon="8.848581"/>
<node id="25779130" lat="53.0331149" lon="8.8469982"/>
<node id="25779131" lat="53.0302414" lon="8.8489081"/>
<node id="25779132" lat="53.0302971" lon="8.8491894"/>
<node id="25779168" lat="53.0314824" lon="8.8497289"/>
<node id="25779170" lat="53.0328814" lon="8.8423081"/>
<tag k="maxspeed" v="30"/>
<tag k="maxspeed:zone" v="yes"/>
</osm>
答案 0 :(得分:2)
由于id也是一个属性,你应该使用@
String expr0 = "//node[@lat=53.0319947]/@id";
未来:
//node/something - You get all the something elements in a node element
//node/@something - You get all the something node's attribute
//node[@lat=53.0319947]/@id" you get all node's id attribute where lat attribute is 53.0319947