Is there any library available to parse KML ?
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!
This library looks promising as well:
http://code.google.com/p/javaapiforkml/
The library provides support till now.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With