2

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


    }
}
4

2 回答 2

1

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

于 2012-07-15T05:24:07.447 回答
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);
        }

它是嵌套的(在您的情况下,先搜索方法,然后搜索这种方式中的节点或这种方式中的标签,但直到现在我还没有找到一个好的解决方案)

希望它有所帮助

于 2013-12-20T21:25:43.983 回答