在java中读取并解析KML

时间:2009-07-16 20:35:35

标签: java parsing kml

是否有可用于解析KML的库?

7 个答案:

答案 0 :(得分:9)

你将创建自己的库,但你不会编写任何代码。

我建议查看http://code.google.com/apis/kml/documentation/kmlreference.html。从那里你可以得到XML Schema。获得模式后,可以使用JAXB生成对象树,以便轻松解析和编写KML。

This may also be a good resource,看起来其他人已经做过了!

答案 1 :(得分:7)

这个库看起来也很有希望:

http://code.google.com/p/javaapiforkml/

图书馆提供支持至今。

答案 2 :(得分:1)

因为它是xml,你可以用任何解析器读取数据,但是http://code.google.com/p/libkml/仍然有一个lib,它有java的绑定但是lib在C ++中

答案 3 :(得分:1)

这是我的JSOUP实现希望它有所帮助

public ArrayList<ArrayList<LatLng>> getCoordinateArrays() {
    ArrayList<ArrayList<LatLng>> allTracks = new ArrayList<ArrayList<LatLng>>();

    try {
        StringBuilder buf = new StringBuilder();
        InputStream json = MyApplication.getInstance().getAssets().open("track.kml");
        BufferedReader in = new BufferedReader(new InputStreamReader(json));
        String str;
                      String buffer;
        while ((str = in.readLine()) != null) {
            buf.append(str);
        }

        in.close();
        String html = buf.toString();
        Document doc = Jsoup.parse(html, "", Parser.xmlParser());
        ArrayList<String> tracksString = new ArrayList<String>();
        for (Element e : doc.select("coordinates")) {
            tracksString.add(e.toString().replace("<coordinates>", "").replace("</coordinates>", ""));
        }

        for (int i = 0; i < tracksString.size(); i++) {
            ArrayList<LatLng> oneTrack = new ArrayList<LatLng>();
            ArrayList<String> oneTrackString = new ArrayList<String>(Arrays.asList(tracksString.get(i).split("\\s+")));
            for (int k = 1; k < oneTrackString.size(); k++) {
                LatLng latLng = new LatLng(Double.parseDouble(oneTrackString.get(k).split(",")[0]),
                        Double.parseDouble(oneTrackString.get(k).split(",")[1]));
                oneTrack.add(latLng);
            }
            allTracks.add(oneTrack);
        }}

    } catch (Exception e) {
        e.printStackTrace();
    }
    return allTracks;
}

答案 4 :(得分:1)

这是其他选项,kml文件是普通文件,其中包含xml文件的结构。 这是另一个示例,用于在文件中搜索多个地标

中的一个特定地标
private static void readKML(InputStream fileKML, String nameCoordinates) {
    String column = null;
    Boolean folder = Boolean.TRUE;
    Boolean placemark = Boolean.FALSE;
    Boolean placeCorrect = Boolean.FALSE;
    BufferedReader br = new BufferedReader(new InputStreamReader(fileKML));
    try {
        while ((column = br.readLine()) != null) {
            if (folder) {
                int ifolder = column.indexOf("<Folder>");
                if (ifolder != -1) {
                    folder = Boolean.FALSE;
                    placemark = Boolean.TRUE;
                    continue;
                }
            }
            if (placemark) {
                String tmpLine = nameCoordinates;
                tmpLine = tmpLine.replaceAll("\t", "");
                tmpLine = tmpLine.replaceAll(" ", "");
                String tmpColumn = column;
                tmpColumn = tmpColumn.replaceAll("\t", "");
                tmpColumn = tmpColumn.replaceAll(" ", "");
                int name = tmpColumn.indexOf(tmpLine);
                if (name != -1) {
                    placemark = Boolean.FALSE;
                    placeCorrect = Boolean.TRUE;
                    continue;
                }
            }
            if (placeCorrect) {
                int coordin = column.indexOf("<coordinates>");
                if (coordin != -1) {
                    String tmpCoordin = column;
                    tmpCoordin = tmpCoordin.replaceAll(" ", "");
                    tmpCoordin = tmpCoordin.replaceAll("\t", "");
                    tmpCoordin = tmpCoordin.replaceAll("<coordinates>", "");
                    tmpCoordin = tmpCoordin
                            .replaceAll("</coordinates>", "");
                    String[] coo = tmpCoordin.split(",");
                    System.out.println("LONG: "+coo[0]);
                    System.out.println("LATI: "+coo[1])
                    break;
                }
            }

        }
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return cresp;
}

答案 5 :(得分:0)

osmbonuspack在处理kml数据时效果非常好。

答案 6 :(得分:0)

如果你使用android studio:)

dependencies {
    compile 'org.jsoup:jsoup:1.8.1'
}


      // find a way to read the file and store it in a string

       String inputFileContents = ""; 
        String xmlContent = inputFileContents;
        Document doc = Jsoup.parse(xml, "", Parser.xmlParser());

        for(Element e : doc.select("LineString").select("coordinates")) {
            // the contents
            System.out.println(e.text());
        }

您可以进行多次select()方法调用。 我将代码简化为:

 Element e = doc.select("LineString").select("coordinates").first();