Skip to content

Instantly share code, notes, and snippets.

@lordgape
Created March 15, 2021 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lordgape/85555d09ffb14725d9656fb92f3ae984 to your computer and use it in GitHub Desktop.
Save lordgape/85555d09ffb14725d9656fb92f3ae984 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