12

Is there any library available to parse KML ?

Evgeny Shepelyuk
  • 129
  • 1
  • 1
  • 4
  • Here's my implementation with Jsoup http://stackoverflow.com/questions/1140144/read-and-parse-kml-in-java/21283827#21283827 – alexandrius Jan 22 '14 at 13:20

7 Answers7

10

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 JAXB to generate an object tree to easily parse and write KML.

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

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
basszero
  • 29,624
  • 9
  • 57
  • 79
  • 1
    I went down this road for a little practice, there's a little hiccup in that there are a couple of elements that collide when the schema is run through the compiler. Nothing major, but be prepared to write a few custom bindings. – CurtainDog Mar 16 '11 at 03:37
7

This library looks promising as well:

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

The library provides support till now.

ArunRaj
  • 1,780
  • 2
  • 26
  • 48
Ajay
  • 763
  • 5
  • 17
2

Here's my JSOUP implementation hope it helps

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;
}
alexandrius
  • 1,097
  • 1
  • 10
  • 21
1

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++

Mite Mitreski
  • 3,596
  • 3
  • 29
  • 39
1

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

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;
}
Carlos JFB
  • 21
  • 1
0

osmbonuspack works really well in case of handling kml data.

RickPat
  • 487
  • 6
  • 6
0

if you use 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:

 Element e = doc.select("LineString").select("coordinates").first();
Pratik
  • 97
  • 1
  • 11
  • Error:(20, 29) java: no suitable method found for parse(org.jsoup.nodes.Document.OutputSettings.Syntax,java.lang.String,org.jsoup.parser.Parser) method org.jsoup.Jsoup.parse(java.lang.String,java.lang.String,org.jsoup.parser.Parser) is not applicable (argument mismatch; org.jsoup.nodes.Document.OutputSettings.Syntax cannot be converted to java.lang.String) method – Kamil Nękanowicz Apr 10 '17 at 07:53
  • my imports: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 Apr 10 '17 at 07:53