gpt4 book ai didi

java - 如何检索属性的值

转载 作者:行者123 更新时间:2023-11-30 08:49:37 25 4
gpt4 key购买 nike

对于下面贴出的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";

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

关于java - 如何检索属性的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31339111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com