/*
* 
* A simple script to create (or print) a JSON file from an XML file
*
*/

var fs = require('fs');
var path = require('path');
var xml2js = require('xml2js');
var parser = new xml2js.Parser();

var output = './';

module.exports = (function () {

    this.publishJSON = function (file, outputDir) {
        parser.parseString(file, function (err, result) {
            if (err) throw err;
            var outputFilename = outputDir + file.split(".xml")[0] + '.json';
            fs.writeFile(outputFilename, JSON.stringify(result, null, 4), function (err) {
                if (err) throw err
                console.log("JSON saved to " + outputFilename);
            });
        });
    }

    this.convertToJSON = function (file, callBack) {
        fs.readFile(file, 'utf-8', function (err, xml) {
            if (err) console.log(err)
            parser.parseString(xml, function (err, result) {
                if (err) callBack("Error!");
                else callBack({"result": result, "file": file});
            });
        });
    }

    this.convertToJSONSync = function (file) {
        fs.readFile(file, 'utf-8', function (err, xml) {
            if (err) console.log(err)
            parser.parseString(xml, function (err, result) {
                if (err);
                else return {"result": result, "file": file};
            });
        });
    }

    return this;
})();

@@ -1,13 +1,12 @@
 /*%0A*
- 
 %0A* A sim
@@ -829,32 +829,52 @@
 onsole.log(err)%0A
+            try %7B%0A  
             pars
@@ -926,32 +926,34 @@
                 
+  
 if (err) callBac
@@ -977,24 +977,26 @@
             
+ 
+ 
 else callBac
@@ -1036,36 +1036,287 @@
 %7D);%0A            
+  
 %7D);%0A
+            %7D%0A            catch (err) %7B%0A              fs.appendFile('xml-with-bad-format.txt', file, function (err) %7B%0A                console.log(%22Finded an XML with a bad format%22);%0A                console.log(file);%0A              %7D);%0A            %7D%0A
         %7D);%0A    

Manage XMLs with bad syntax