Created
March 15, 2021 20:14
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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