Skip to content

Instantly share code, notes, and snippets.

@hellotunmbi
Forked from lordgape/parser.js
Created September 17, 2022 18:20
Show Gist options
  • Save hellotunmbi/3e2a5e9f683734c07cc7c90921100fc4 to your computer and use it in GitHub Desktop.
Save hellotunmbi/3e2a5e9f683734c07cc7c90921100fc4 to your computer and use it in GitHub Desktop.
A node utility class to convert xml to json and vice versa. Depends on xml2js and jsontoxml node package
const jsonxml = require("jsontoxml");
const parseString = require("xml2js").parseString;
const { promisify } = require("util");
const promisfiedParseString = promisify(parseString);
module.exports = class Parser {
static parseJSONBodyToXML(jsonArgument) {
return jsonxml(jsonArgument, { html: true });
}
static async convertXMLToJSON(xmlMessage) {
const options = { trim: true, explicitArray: false, explicitRoot: false };
return promisfiedParseString(xmlMessage, options);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment