xml2js.js 550 B

1234567891011121314151617181920212223242526272829
  1. 'use strict';
  2. const xml2js = require('xml2js');
  3. const parseXml = str => {
  4. return new Promise((resolve, reject) => {
  5. const parseString = xml2js.parseString;
  6. parseString(str, { explicitArray: false }, (err, json) => {
  7. if (json) {
  8. resolve(json.xml);
  9. } else {
  10. reject(err);
  11. }
  12. });
  13. });
  14. };
  15. const createXml = obj => {
  16. const builder = new xml2js.Builder({
  17. rootName: 'xml',
  18. headless: true,
  19. cdata: true,
  20. });
  21. return builder.buildObject(obj);
  22. };
  23. module.exports = {
  24. parseXml,
  25. createXml,
  26. };