Thursday, 8 June 2017

Deep level xml parsing to csv using NodeJS

I have a medium sized xml ~ 5mb that needs to be converted to csv.

Obviously wont go for reinventing the wheel, so a two layer approach - 1> xml to json 2> json to csv

My current code is :

const xml_obj = {}
const htt = require('http-status-code-node');
var fs = require('fs'); 
var xml2js = require('xml2js');
var converter = require('json-2-csv'); 

xml_obj["convert"] = (req, res, next) => { 
  var parser = new xml2js.Parser();
  fs.readFile(__dirname + '/directoryexport.xml', function (err, data) {   
    parser.parseString(data, function (err, result) { 
      console.log('Done'); 
      var callback = function (err, ycsv) {
        if (err) return console.log(err); 
        ///
        res.setHeader('Content-Disposition', 'attachment; filename=testing.csv');
        res.set('Content-Type', 'text/csv');
        res.status(200).send(result);
        ///
      }
      var documents = [];      
      documents.push(result)
      converter.json2csv(documents, callback);      
    })
  });
} 
module.exports = xml_obj.convert

However the xml being nested gives a multi layered json which the yields a single string instead of a proper delimited csv..

The current output CSV

The Original xml

The XML structure

The Json I get on converting xml

Also as per the documentation of the json to csv converter if the input json is in a proper structure like :

[
    {
        Make: 'Nissan',
        Model: 'Murano',
        Year: '2013',
        Specifications: {
            Mileage: '7106',
            Trim: 'S AWD'
        }
    },
    {
        Make: 'BMW',
        Model: 'X5',
        Year: '2014',
        Specifications: {
            Mileage: '3287',
            Trim: 'M'
        }
    }
];

This yields a very nicely formatted csv like this : Example Perfect CSV From JSON

Any tips on how to make this work with my input.



via Saleem Ahmed

No comments:

Post a Comment