在 Java 中读取和解析 KML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1140144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:12:41  来源:igfitidea点击:

Read and parse KML in java

javaparsingkml

提问by Evgeny Shepelyuk

Is there any library available to parse KML ?

是否有任何库可用于解析 KML?

回答by basszero

You'll be making your own library, but you won't be writing any code.

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

I suggest looking at http://code.google.com/apis/kml/documentation/kmlreference.html. From there you can get the XML Schema. Once you've got the schema you can use JAXBto generate an object tree to easily parse and write KML.

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

This may also be a good resource, looks like someone else has already done it!

这也可能是一个很好的资源,看起来已经有人做了!

回答by Mite Mitreski

Since it is xml you can read the data with any parser but still there is an lib available at http://code.google.com/p/libkml/it has bindings for java but the lib is in C++

由于它是 xml,因此您可以使用任何解析器读取数据,但在http://code.google.com/p/libkml/ 上仍然有一个可用的库,它具有 Java 的绑定,但该库是用 C++ 编写的

回答by Ajay

This library looks promising as well:

这个库看起来也很有前途:

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

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

The library provides support till now.

该库提供支持至今。

回答by alexandrius

Here's my JSOUP implementation hope it helps

这是我的 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;
}

回答by RickPat

osmbonuspackworks really well in case of handling kml data.

在处理 kml 数据的情况下,osmbonuspack工作得非常好。

回答by Pratik

if you use android studio :)

如果您使用 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());
        }

You can have multiple select() method calls. I simplified the code to:

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

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

回答by Carlos JFB

This is other options, the kml file is a normal file, who contain structure the xml file. This is other example, for search one specific placemark in the file the multiple placemarks

这是其他选项,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;
}