I found an amazing package to make it very simple.
xml2js
For me, I do this in an angular 2 application, but on the node side.
npm install xml2js
It is literally as simple as passing xml like this,
var parseString = require('xml2js').parseString;
var xml = "<root>Hello xml2js!</root>"
parseString(xml, function (err, result) {
console.dir(result);
});
In my application, I had an xml file and it used it,
var fs = require('fs');
var parseString = require('xml2js').parseString;
function requestCreditReport(callback) {
fs.readFile('./credit-api/response.xml', 'utf8', function (err,data) {
if (err) return callback(err);
parseString(data, callback);
});
}
Check out this jsfiddle
Hope this helps.
source
share