fast-xml2json
Version:
C++ addon to convert xml files to json files or json sting
55 lines (49 loc) • 1.75 kB
JavaScript
var xml2json = require('./build/Release/xml2json.node');
//var xmlFilePath = '/Users/alexpua/Sites/circlemedia/FLData/paciolan/old/SDM_RUTGERS_17067_68402.xml';
//var jsonFilePath = '/Users/alexpua/Sites/circlemedia/FLData/paciolan/old/SDM_RUTGERS_17067_68402.json';
//
//xml2json.convertToFile(xmlFilePath, jsonFilePath, function (err, result) {
// console.log(err);
// console.log('convertToFile');
// console.log(xmlFilePath);
// console.log(jsonFilePath);
// console.log(result);
//});
//
//xml2json.convertFile(xmlFilePath, jsonFilePath, function (err, result) {
// console.log(err);
// console.log('convertFile');
// console.log(xmlFilePath);
// console.log(jsonFilePath);
// console.log(result);
//});
//
//xml2json.convertToJson(xmlFilePath, function (err, result) {
// console.log(err);
// console.log('convertToJson');
// console.log(xmlFilePath);
// console.log(result);
//});
//var xml2json = require('fast-xml2json');
var async = require('async');
var fs = require('fs');
var path = require('path');
var xmlFilesPath = '/Users/alexpua/Sites/circlemedia/FLData/paciolan/data/',
count = 0,
filesArr;
filesArr = fs.readdirSync(xmlFilesPath);
if(filesArr.length > 0) {
async.eachSeries(filesArr, function (xmlFile, callback) {
if(path.extname(xmlFile) != '.xml' ) return callback();
xmlFile = path.join(xmlFilesPath, xmlFile);
xml2json.convertToJson(xmlFile, function (err, result) {
console.log(++count + ': ' + xmlFile);
console.log(result.length);
callback();
});
}, function (err) {
// do something
});
} else {
console.log('Directory ' + xmlFilesPath + ' doesn\'t have xml files' );
}