In my home task, I was asked to develop Restful api on server Node.js. My problem is to convert huge XML into JSON format by using URL.
I tried to use different directories like xml-stream, xml2json. And I found on the net a function that would do it for me, but the result was strange. I used the following guide: https://antrikshy.com/blog/fetch-xml-url-convert-to-json-nodejs
var express = require('express');
var router = express.Router();
// Parse XML to JSON.
var parseString = require('xml2js').parseString;
var https = require('https');
/* GET home page. */
router.get('/', function(req, res, next) {
function xmlToJson(url, callback) {
var req = https.get(url, function(res) {
var xml = '';
res.on('data', function(chunk) {
xml += chunk;
});
res.on('error', function(e) {
callback(e, null);
});
res.on('timeout', function(e) {
callback(e, null);
});
res.on('end', function() {
parseString(xml, function(err, result) {
callback(null, result);
});
});
});
}
var url = "https://www.boi.org.il/he/BankingSupervision/BanksAndBranchLocations/Lists/BoiBankBranchesDocs/snifim_dnld_he.xml";
xmlToJson(url, function(err, data) {
if (err) {
return console.err(err);
}
res.json(JSON.stringify(data, null, 2));
});
});
module.exports = router;
I expected a valid JSON output and got the wrong output.
"{\n \"BRANCHES\": {\n \"$\": {\n \"xmlns:ns0\": \"BOI_snifim_he\"\n },\n \"BRANCH\": [\n {\n \"Bank_Code\": [\n \"13\"\n ],\n \"Bank_Name\": [\n \"13001-בנק אגוד לישראל בע\\\"מ\"\n ],\n \"Branch_Code\": [\n \"142\"\n ],\n \"Branch_Name\": [\n \"מבשרת\"\n ],\n \"Branch_Address\": [\n \"החושן 6 9079562\"\n ],\n \"City\": [\n \"מבשרת ציון\"\n ],\n \"Zip_Code\": [\n \"9079562\"\n ],\n \"POB\": [\n \"\"\n ],\n \"Telephone\": [\n \"1-599-599-142\"\n ],\n \"Fax\": [\n \"02-5706001\"\n ],\n \"Free_Tel\": [\n \"\"\n ],\n \"Handicap_Access\": [\n \"כן\"\n ],\n \"day_closed\": [\n \"יום א\"\n ],\n \"Branch_Type\": [\n \"מיוחד\"\n ],\n \"Date_Open\": [\n \"02/11/2004\"\n ],\n \"Date_Closed\": [\n \"26/12/2016\"\n ],\n \"Merge_Bank\": [\n \"\"\n ],\n \"Merge_Branch\": [\n \"\"\n ],\n \"X_Coordinate\": [\n \"\"\n ],\n \"Y_Coordinate\": [\n \"\"\n ]\n },\n {\n \"Bank_Code\": [\n \"13\"\n ],\n \"Bank_Name\": [\n \"13001-בנק אגוד לישראל בע\\\"מ\"\n ],\n \"Branch_Code\": [\n \"123\"\n ],\n \"Branch_Name\": [\n \"אגוד ישיר\"\n ],\n \"Branch_Address\": [\n \"אבא הילל סלבר 13 \"\n ],\n