Amrmsmb
Amrmsmb

Reputation: 1

how to retrieve the value of an attribute

For the XML posted below, I want to get the value of the id corresponding to the lat = 53.0319947.

To achieve this I've written the following code. Unfortunately I didn't get any results. Please let me know how to solve it:

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>

Upvotes: 2

Views: 49

Answers (1)

crashxxl
crashxxl

Reputation: 679

As id is an attribute too, you should use @

String expr0 = "//node[@lat=53.0319947]/@id";

For the future:

//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

Upvotes: 2

Related Questions