Skip to content

Instantly share code, notes, and snippets.

@timvw
Created February 14, 2013 03:20
Show Gist options
  • Save timvw/4950357 to your computer and use it in GitHub Desktop.
Save timvw/4950357 to your computer and use it in GitHub Desktop.
var parser = require('xml2json');
var fs = require('fs');
var cwd = process.cwd();
fs.readdir(cwd, function(err, files) {
if(err){
console.log(err);
return;
}
var last = files.length;
for(var i=0; i<last; ++i){
try {
var file = cwd + '/' + files[i];
var xml = fs.readFileSync(file, 'utf8');
var json = parser.toJson(xml);
var newFile = cwd + '/json/' + files[i] + '.json';
fs.writeFileSync(newFile, json);
} catch(e) {
console.log('failed to convert ' + file + ' to: ' + newFile + ' because: ' + e);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment