# Loading KML from raw text

**URL:** <https://community.cesium.com/t/loading-kml-from-raw-text/9992>\
**Category:** CesiumJS\
**Created:** [June 23, 2020, 10:28pm UTC](https://community.cesium.com/t/loading-kml-from-raw-text/9992 "2020-06-23T22:28:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lunatic-programmer](https://avatars.discourse-cdn.com/v4/letter/l/3be4f8/32.png) [@lunatic-programmer](https://community.cesium.com/u/lunatic-programmer)\
**Post date:** [June 23, 2020, 10:28pm UTC](https://community.cesium.com/t/loading-kml-from-raw-text/9992/1 "2020-06-23T22:28:58Z")

</div>

I am work on a project where I need to load kml files from the client computer. They select the file and its read into a buffer on the browser. The documentation said I could load the kml into the scene directly from a raw kml string, but when I try to do it, it errors saying

```auto
"Cesium.js:1 Uncaught TypeError: Cannot read property '1' of null
    at new URI (Cesium.js:1)
    at new Resource (Cesium.js:1)
    at Function.Resource.createIfNeeded (Cesium.js:1)
    at load$2 (Cesium.js:1)
    at KmlDataSource.load (Cesium.js:1)
    at Function.KmlDataSource.load (Cesium.js:1)
    at AppState.addKMLFromText (app.js:95)
    at addKMLToScene (object-loader.js:47)
    at parseClientLoadedObject (object-loader.js:32)
    at FileReader.reader.onload (client-files.js:27)

```

The KML file loads correctly when its loaded from the server using a url. I have also verified that the file text has been loaded correctly.

```auto
	addKMLFromText(fileText) {
		var kmlOptions = {
			camera : appState.getViewer().scene.camera,
			canvas : appState.getViewer().scene.canvas,
			clampToGround : true
		};
		
		appState.viewer.dataSources.add(Cesium.KmlDataSource.load(fileText, kmlOptions));
	}

```

I would appreciate any and all help with this issue.

---

<div class="post-metadata">

**Author:** ![omar](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/omar/32/1999_2.png) [@omar](https://community.cesium.com/u/omar)\
**Post date:** [June 24, 2020, 8:25pm UTC](https://community.cesium.com/t/loading-kml-from-raw-text/9992/2 "2020-06-24T20:25:04Z")

</div>

This will not work because the [KMLDataSource.load](https://cesium.com/docs/cesiumjs-ref-doc/KmlDataSource.html?classFilter=KMLD#.load) function does not accept a KML string. That argument you pass in must be:

> A url, parsed KML document, or Blob containing binary KMZ data or a parsed KML document.

If you’ve already fetched the text and want to load it, you’ll need to pass it to DOMParser first:

```auto
var parser = new DOMParser();
var kmlDoc = parser.parseFromString(kmlContent, "text/xml");
viewer.zoomTo(viewer.dataSources.add(Cesium.KmlDataSource.load(kmlDoc, options)))

```

Here’s a [full Sandcastle example](https://sandcastle.cesium.com/index.html#c=fVVtj5s4EP4rVu5DEykFA7aBLaV36puq7l4jbXr3JR/qgLNBa3Bkm2TT0/33G3CSJdnm+AB4PM888+LxbLlG20rshEZvUSN26L0wVVt7f/Wy8auiX75XjeVVI/SrKfpn0SB4rNAaRDOttlUp9M0RWGjBrfhbaVnOncp4smj+nbxZNFvgUhtbqcYA2cFOwWuhObo5eOGZQjTCc9LpUQWg5qVKJz2qSF5v5uqzVm1T3iCrWwGkwOlYH2vZhSAaC8Q/sndPtURboQ248nYxCjy8GCHRFKqsmgcQfJ9/ep0sRu/yRZMBFIF6Y0C+tnZz4/u73c5TG9E8VMZrhPVBxQ+9cDHKO2eyD6poa6DKnWtZA7HkM27XJvP7/4O8FKbQVZ+P/OMT+C+FQWqFNp2qh/5UViC75hZe8COMEVLyTsYfUGXQco9KseKttM4eQkaArkLYQ19WaK9atONNL3E1GdgokYRqmmlneo/q1li0FIi3dq20KI/2xkojUVagPkFlpUVh5R5VDfp6d+tl/tD9Q0T3di8FqkpI1R6I1O4WWD5rIZqZkvtDfnrNbqPXPolAWCipdB6vMF7Bk/luPVDYVaVd5yTz3c/Jmv/SXNYx/i8DxpcMmX8Jyvyz1UzyQtRcPz5D+oL+sTRKtpDhj09w8kpRnhX6otRzzRuz4bo7jA9dbqBMUqJdZdfIZQ2p1vb1+VWSwZrpfPquZf7bL7Kc+afti2xrONzDZHBpKwve3qlS5GcNlPlnewPM8xHKg8wfrM6SrDQ0EkhNjl4HAbQGpThOaBTFcRpMI+bh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