xml2customjs
Version:
Library to custom convert an XML into a Javascript object or JSON
66 lines • 3.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertXmlToJson = exports.convertXmlToJs = void 0;
const xmlbuilder2_1 = require("xmlbuilder2");
const utils_1 = require("./utils");
/**
* This function converts an XML into a JS object according to
* a given set of options
* @param xml string
* @param options XmlToJsOptionalOptions
* Options:
* - arrayFields: array containing the names of the XML tags that are supposed to be arrays (example ['propertyKey'])
* - objectFields: array containing the names of the XML tags that are supposed to be objects (example ['propertyKey']),
* This is useful in the case of XML tags that may have properties. Read the documentation for more detail on this feature.
* - fieldNameFormat: option to format all property keys. By default, this will be set to "none".
* Options are: "camel" for camelcase format, "snake" for snakecase and "none" for no formatting.
* - fieldNameMapping: this object will be used to change the name of the property keys. The keys of this
* object need to be the exact name (in the original format) of the XML tag, and the value will be the name used
* to replace the property key during the convertion
* @returns object
*/
function convertXmlToJs(xml, options = {}) {
var _a, _b, _c, _d;
const optionsObj = {
arrayFields: (_a = options.arrayFields) !== null && _a !== void 0 ? _a : [],
objectFields: (_b = options.objectFields) !== null && _b !== void 0 ? _b : [],
fieldNameFormat: (_c = options.fieldNameFormat) !== null && _c !== void 0 ? _c : 'none',
fieldNameMapping: (_d = options.fieldNameMapping) !== null && _d !== void 0 ? _d : {}
};
const convertedObject = xmlbuilder2_1.convert(xml, { format: 'object' });
const clonedObject = JSON.parse(JSON.stringify(convertedObject));
const mappedObject = utils_1.transformObject(clonedObject, optionsObj);
return mappedObject;
}
exports.convertXmlToJs = convertXmlToJs;
/**
* This function converts an XML into a JSON according to
* a given set of options
* @param xml string
* @param options XmlToJsOptionalOptions
* Options:
* - arrayFields: array containing the names of the XML tags that are supposed to be arrays (example ['propertyKey'])
* - objectFields: array containing the names of the XML tags that are supposed to be objects (example ['propertyKey']),
* This is useful in the case of XML tags that may have properties. Read the documentation for more detail on this feature.
* - fieldNameFormat: option to format all property keys. By default, this will be set to "none".
* Options are: "camel" for camelcase format, "snake" for snakecase and "none" for no formatting.
* - fieldNameMapping: this object will be used to change the name of the property keys. The keys of this
* object need to be the exact name (in the original format) of the XML tag, and the value will be the name used
* to replace the property key during the convertion
* @returns JSON string
*/
function convertXmlToJson(xml, options = {}) {
var _a, _b, _c, _d;
const optionsObj = {
arrayFields: (_a = options.arrayFields) !== null && _a !== void 0 ? _a : [],
objectFields: (_b = options.objectFields) !== null && _b !== void 0 ? _b : [],
fieldNameFormat: (_c = options.fieldNameFormat) !== null && _c !== void 0 ? _c : 'none',
fieldNameMapping: (_d = options.fieldNameMapping) !== null && _d !== void 0 ? _d : {}
};
const convertedObject = xmlbuilder2_1.convert(xml, { format: 'object' });
const clonedObject = JSON.parse(JSON.stringify(convertedObject));
const mappedObject = utils_1.transformObject(clonedObject, optionsObj);
return JSON.stringify(mappedObject);
}
exports.convertXmlToJson = convertXmlToJson;
//# sourceMappingURL=index.js.map