我有一个xpath查询,我可以获取所有属性及其各自的值
但现在我想知道这些属性属于哪个元素?
这是我的xml:
<?xml version='1.0' encoding='UTF-8'?>
<osm version="0.6" generator="osmconvert 0.5Z">
<bounds minlat="51.60542" minlon="-0.1854331" maxlat="51.69193" maxlon="-0.0088394"/>
<node id="195870" lat="51.6844148" lon="-0.1772914" version="4" timestamp="2008-10-07T19:42:43Z" changeset="136213" uid="508" user="Welshie">
<tag k="highway" v="motorway_junction"/>
<tag k="ref" v="24"/>
</node>
<node id="33206602" lat="51.6084884" lon="-0.0365496" version="4" timestamp="2011-09-23T16:54:00Z" changeset="9378015" uid="28024" user="dbisping">
<tag k="amenity" v="fuel"/>
<tag k="fuel:biodiesel" v="yes"/>
<tag k="fuel:biogas" v="no"/>
<tag k="fuel:cng" v="no"/>
<tag k="fuel:diesel" v="no"/>
<tag k="fuel:hydrogen" v="no"/>
<tag k="fuel:lpg" v="no"/>
<tag k="fuel:octane_100" v="no"/>
<tag k="fuel:octane_95" v="no"/>
<tag k="fuel:octane_98" v="no"/>
<tag k="name" v="Pure Fuels"/>
<tag k="self_service" v="no"/>
</node>
</osm>
和我写的代码:
import com.ximpleware.*;
public class Test2
{
public static void main (String[] abc)
{
VTDGen vg = new VTDGen();
vg.parseFile("C:\\workspace\\sample osm xml\\1.xml",false);
VTDNav nav = vg.getNav();
AutoPilot ap = new AutoPilot(nav);
try {
//ap.selectXPath("/osm/node/tag[@k='amenity']");
ap.selectXPath("//node/tag");
while (ap.evalXPath() != -1 )
{
//getting attribute first element
int val = nav.getAttrVal("k");
if (val != -1)
{
String element = "The element or node is ??? ";
String o = "key = " + nav.toNormalizedString(val) + " value = " +nav.toString(val + 2);
System.out.print(element);
System.out.println(o);
}
}
} catch (XPathParseException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (XPathEvalException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (NavException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
答案 0 :(得分:1)
vn.toElement(vn.PARENT)怎么样?
答案 1 :(得分:1)
我也有同样的问题,我无法处理它(我也在研究OSM数据)
我使用了一种方法,但我认为它对OSM等大量数据无效。
我正在尝试以某种方式获取节点,当我看到节点时,我尝试获取其父ID
int wayIndex = 1;
while (way_id_counter != -1)
{
start = System.currentTimeMillis();
wayID = vn.toString(way_id_counter+1);
apSubNode_id.resetXPath();
apSubNode_id.selectXPath("//way[" + wayIndex +"]/nd/@ref");
way_node_id_counter = apSubNode_id.evalXPath();
while (way_node_id_counter != -1)
{
subNodeID = vn.toString(way_node_id_counter+1);
//System.out.println("Sub node id is " + subNodeID);
way_node_id_counter = apSubNode_id.evalXPath();
}
wayIndex++;
way_id_counter = apWay_id.evalXPath();
//System.out.println("Way id is " + wayID);
//break;
end = System.currentTimeMillis();
System.out.println(end - start);
}
它是嵌套的(在这种情况下先用这种方式搜索节点或用这种方式标记,但到目前为止我找不到一个好的解决方案)
希望它有所帮助