-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert.js
48 lines (41 loc) · 1.37 KB
/
convert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
*
* 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({"file": file});
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;
})();