gpt4 book ai didi

java - 如何获取 vtd-xml 中的父元素?

转载 作者:行者123 更新时间:2023-11-30 09:36:16 25 4
gpt4 key购买 nike

我有一个 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.
}


}
}

最佳答案

vn.toElement(vn.PARENT) 怎么样?

关于java - 如何获取 vtd-xml 中的父元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10832662/

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