/
xml.ts
97 lines (91 loc) · 3.05 KB
/
xml.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import * as xml2js from "xml2js";
import { XML_ATTRKEY, XML_CHARKEY, SerializerOptions } from "./serializer.common";
// Note: The reason we re-define all of the xml2js default settings (version 2.0) here is because the default settings object exposed
// by the xm2js library is mutable. See https://github.com/Leonidas-from-XIV/node-xml2js/issues/536
// By creating a new copy of the settings each time we instantiate the parser,
// we are safeguarding against the possibility of the default settings being mutated elsewhere unintentionally.
const xml2jsDefaultOptionsV2: xml2js.OptionsV2 = {
explicitCharkey: false,
trim: false,
normalize: false,
normalizeTags: false,
attrkey: XML_ATTRKEY,
explicitArray: true,
ignoreAttrs: false,
mergeAttrs: false,
explicitRoot: true,
validator: undefined,
xmlns: false,
explicitChildren: false,
preserveChildrenOrder: false,
childkey: "$$",
charsAsChildren: false,
includeWhiteChars: false,
async: false,
strict: true,
attrNameProcessors: undefined,
attrValueProcessors: undefined,
tagNameProcessors: undefined,
valueProcessors: undefined,
rootName: "root",
xmldec: {
version: "1.0",
encoding: "UTF-8",
standalone: true
},
doctype: undefined,
renderOpts: {
pretty: true,
indent: " ",
newline: "\n"
},
headless: false,
chunkSize: 10000,
emptyTag: "",
cdata: false
};
// The xml2js settings for general XML parsing operations.
const xml2jsParserSettings: any = Object.assign({}, xml2jsDefaultOptionsV2);
xml2jsParserSettings.explicitArray = false;
// The xml2js settings for general XML building operations.
const xml2jsBuilderSettings: any = Object.assign({}, xml2jsDefaultOptionsV2);
xml2jsBuilderSettings.explicitArray = false;
xml2jsBuilderSettings.renderOpts = {
pretty: false
};
/**
* Converts given JSON object to XML string
* @param obj - JSON object to be converted into XML string
* @param opts - Options that govern the parsing of given JSON object
*/
export function stringifyXML(obj: unknown, opts: SerializerOptions = {}): string {
xml2jsBuilderSettings.rootName = opts.rootName;
xml2jsBuilderSettings.charkey = opts.xmlCharKey ?? XML_CHARKEY;
const builder = new xml2js.Builder(xml2jsBuilderSettings);
return builder.buildObject(obj);
}
/**
* Converts given XML string into JSON
* @param str - String containing the XML content to be parsed into JSON
* @param opts - Options that govern the parsing of given xml string
*/
export function parseXML(str: string, opts: SerializerOptions = {}): Promise<any> {
xml2jsParserSettings.explicitRoot = !!opts.includeRoot;
xml2jsParserSettings.charkey = opts.xmlCharKey ?? XML_CHARKEY;
const xmlParser = new xml2js.Parser(xml2jsParserSettings);
return new Promise((resolve, reject) => {
if (!str) {
reject(new Error("Document is empty"));
} else {
xmlParser.parseString(str, (err?: Error, res?: any) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
}
});
}