The next-generation blog, news, and article for you to start sharing your stories today!
Nodejs does not support internal xml to json convert functionality. To achieve this task we have a popular nodejs library that is ..
Also we need a helper function to convert json to xml.
See the below code to understand the conversion logic
const parser = require('fast-xml-parser');
class Helper {
constructor() {
}
jsonToXml (json,config = null) {
let options = {
attributeNamePrefix : "@",
textNodeName : "$",
ignoreAttributes : false,
tagValueProcessor: a=> he.encode(a.toString(), { useNamedReferences: false})
};
const jsparser = new parser.j2xParser(options);
json = {xml:json};
let xml = jsparser.parse(json);
return '<?xml version="1.0" encoding="utf-8"?>' + xml;
}
}
module.exports = new Helper();
Here is simple code for convert your json object in to the xml one.
Well below code of bunch is demostrate that how to use jsonToXml conversion function.
const Helper = require('../your-class-path/Helper') const jsonObj = { "key" : 'your json obj' }; let xmlString = Helper.jsonToXml(jsonObj);
So using this simple technique you can achive your goal.
Hello I am Vikram Parihar, So we have learnt how to write simple json to xml conversion code in nodejs. Feel free to write comment and feedback.
Vikram Parihar is an senior software engineer. He is working since 2013 in web development technologies. He works in NodeJS, Angular, Vue, PHP, Laravel, Express JS and various popular technologies.