Java 如何检索属性的值

Java 如何检索属性的值,java,dom,xpath,sax,Java,Dom,Xpath,Sax,对于下面发布的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.p

对于下面发布的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>

由于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