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