UNPKG

908 BPlain TextView Raw
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License. See License.txt in the project root for license information.
3
4import * as xml2js from "xml2js";
5
6export function stringifyXML(obj: any, opts?: { rootName?: string }) {
7 const builder = new xml2js.Builder({
8 rootName: (opts || {}).rootName,
9 renderOpts: {
10 pretty: false,
11 },
12 });
13 return builder.buildObject(obj);
14}
15
16export function parseXML(str: string): Promise<any> {
17 const xmlParser = new xml2js.Parser({
18 explicitArray: false,
19 explicitCharkey: false,
20 explicitRoot: false,
21 });
22 return new Promise((resolve, reject) => {
23 if (!str) {
24 reject(new Error("Document is empty"));
25 } else {
26 xmlParser.parseString(str, (err?: Error, res?: any) => {
27 if (err) {
28 reject(err);
29 } else {
30 resolve(res);
31 }
32 });
33 }
34 });
35}