placemark / togeojson

convert KML, TCX, and GPX to GeoJSON, without the fuss
https://placemark.github.io/togeojson/
BSD 2-Clause "Simplified" License
409 stars 67 forks source link

This should complain if the input is not valid XML or valid KML #85

Open tmcw opened 2 years ago

tmcw commented 2 years ago

Options:

Raruto commented 1 year ago

Here are a few lines useful to achieve this:

if (data.indexOf("<") != 0) {
  throw 'Invalid XML';
}

let xml  = (new DOMParser()).parseFromString(data, "text/xml");

let type = xml.documentElement.tagName.toLowerCase(); // "kml" or "gpx"

if (xml.getElementsByTagName('parsererror').length) {
  throw 'Invalid XML';
}

Source: leaflet-elevation/src/control.js#L956-L975

👋 Raruto