在Java中读取和解析KML文件

12

有没有可用来解析 KML 的库?


这是我的Jsoup实现代码 https://dev59.com/-nM_5IYBdhLWcg3w-4jw#21283827 - alexandrius
7个回答

10

1
我走了这条路进行一些练习,但是在模式经过编译器时会有一些小问题,因为有几个元素会发生冲突。虽然不是什么大问题,但你需要准备写一些自定义绑定。 - CurtainDog

7

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

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;
}

1
这里缺少分号:System.out.println("LATI: "+coo[1])。 - Kamil Nękanowicz

1

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


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();

错误:(20, 29) java: 没有适合的方法来解析(org.jsoup.nodes.Document.OutputSettings.Syntax,java.lang.String,org.jsoup.parser.Parser) 方法 org.jsoup.Jsoup.parse(java.lang.String,java.lang.String,org.jsoup.parser.Parser) 不适用 (参数不匹配;org.jsoup.nodes.Document.OutputSettings.Syntax 无法转换为 java.lang.String) 方法 - Kamil Nękanowicz
我的导入: import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.parser.Parser; import static org.jsoup.nodes.Document.OutputSettings.Syntax.xml; - Kamil Nękanowicz

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接