xml.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. import * as xml2js from "xml2js";
  4. export function stringifyXML(obj, opts) {
  5. var builder = new xml2js.Builder({
  6. explicitArray: false,
  7. explicitCharkey: false,
  8. rootName: (opts || {}).rootName,
  9. renderOpts: {
  10. pretty: false
  11. }
  12. });
  13. return builder.buildObject(obj);
  14. }
  15. export function parseXML(str) {
  16. var xmlParser = new xml2js.Parser({
  17. explicitArray: false,
  18. explicitCharkey: false,
  19. explicitRoot: false
  20. });
  21. return new Promise(function (resolve, reject) {
  22. if (!str) {
  23. reject(new Error("Document is empty"));
  24. }
  25. else {
  26. xmlParser.parseString(str, function (err, res) {
  27. if (err) {
  28. reject(err);
  29. }
  30. else {
  31. resolve(res);
  32. }
  33. });
  34. }
  35. });
  36. }
  37. //# sourceMappingURL=xml.js.map