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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50import { readFile } from 'fs-extra';
import xml2js from 'xml2js';
export async function readXML(path: string): Promise<any> {
try {
const xmlStr = await readFile(path, { encoding: 'utf-8' });
try {
return await xml2js.parseStringPromise(xmlStr);
} catch (e: any) {
throw `Error parsing: ${path}, ${e.stack ?? e}`;
}
} catch (e) {
throw `Unable to read: ${path}`;
}
}
export function parseXML(xmlStr: string, options?: xml2js.OptionsV2): any {
const parser = options !== undefined ? new xml2js.Parser({ ...options }) : new xml2js.Parser();
let xmlObj;
parser.parseString(xmlStr, (err: any, result: any) => {
if (!err) {
xmlObj = result;
}
});
return xmlObj;
}
export async function writeXML(object: any): Promise<any> {
return new Promise((resolve) => {
const builder = new xml2js.Builder({
headless: true,
explicitRoot: false,
rootName: 'deleteme',
});
let xml = builder.buildObject(object);
xml = xml.replace('<deleteme>', '').replace('</deleteme>', '');
resolve(xml);
});
}
export function buildXmlElement(configElement: any, rootName: string): string {
const builder = new xml2js.Builder({
headless: true,
explicitRoot: false,
rootName: rootName,
});
return builder.buildObject(configElement);
}