-
Notifications
You must be signed in to change notification settings - Fork 353
/
loadXML.ts
31 lines (26 loc) · 970 Bytes
/
loadXML.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import Resource from "terriajs-cesium/Source/Core/Resource";
async function loadXML(
urlOrResource: string | Resource
): Promise<XMLDocument | undefined> {
var resource = (Resource as any).createIfNeeded(urlOrResource) as Resource;
const response = await resource.fetchXML();
/**
* Sometimes Cesium's Resource.fetchXML will return an Array Buffer (usually in Node.js env)
* Adapted from https://github.com/fengyuanchen/is-array-buffer
* The MIT License (MIT)
* Copyright 2015-present Chen Fengyuan
*
* See full license here https://github.com/fengyuanchen/is-array-buffer/blob/main/LICENSE
*/
if (
response instanceof ArrayBuffer ||
toString.call(response) === "[object ArrayBuffer]"
) {
const enc = new TextDecoder("utf-8");
const xmlString = enc.decode(response as any);
const parser = new DOMParser();
return parser.parseFromString(xmlString, "text/xml");
}
return response;
}
export default loadXML;