forked from NaturalIntelligence/fast-xml-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·100 lines (97 loc) · 3.26 KB
/
cli.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env node
"use strict";
/*eslint-disable no-console*/
const fs = require("fs");
const path = require("path");
const parser = require("./lib/parser");
const readToEnd = require("./src/read").readToEnd;
if (process.argv[2] === "--help" || process.argv[2] === "-h") {
console.log("Fast XML Parser " + require(path.join(__dirname + "/package.json")).version);
console.log("----------------");
console.log("xml2js [-ns|-a|-c|-v|-V] <filename> [-o outputfile.json]");
console.log("cat xmlfile.xml | xml2js [-ns|-a|-c|-v|-V] [-o outputfile.json]");
console.log("-ns: remove namespace from tag and atrribute name.");
console.log("-a: don't parse attributes.");
console.log("-c: parse values to premitive type.");
console.log("-v: validate before parsing.");
console.log("-V: validate only.");
} else if (process.argv[2] === "--version") {
console.log(require(path.join(__dirname + "/package.json")).version);
} else {
const options = {
ignoreNameSpace: true,
ignoreAttributes: false,
parseNodeValue: true,
parseAttributeValue: true
};
let fileName = "";
let outputFileName;
let validate = false;
let validateOnly = false;
for (let i = 2; i < process.argv.length; i++) {
if (process.argv[i] === "-ns") {
options.ignoreNameSpace = false;
} else if (process.argv[i] === "-a") {
options.ignoreAttributes = true;
} else if (process.argv[i] === "-c") {
options.parseNodeValue = false;
options.parseAttributeValue = false;
} else if (process.argv[i] === "-o") {
outputFileName = process.argv[++i];
} else if (process.argv[i] === "-v") {
validate = true;
} else if (process.argv[i] === "-V") {
validateOnly = true;
} else {//filename
fileName = process.argv[i];
}
}
const callback = function(xmlData) {
let output = "";
if (validate) {
const result = parser.validate(xmlData);
if (result === true) {
output = JSON.stringify(parser.parse(xmlData, options), null, 4);
} else {
output = result;
}
} else if (validateOnly) {
output = parser.validate(xmlData);
} else {
output = JSON.stringify(parser.parse(xmlData, options), null, 4);
}
if (outputFileName) {
writeToFile(outputFileName, output);
} else {
console.log(output);
}
};
try {
if (!fileName) {
readToEnd(process.stdin, function(err, data) {
if (err) {
throw err;
}
callback(data);
});
} else {
fs.readFile(fileName, function(err, data) {
if (err) {
throw err;
}
callback(data);
});
}
}
catch (e) {
console.log("Seems an invalid file or stream." + e);
}
}
function writeToFile(fileName, data) {
fs.writeFile(fileName, data, function(err) {
if (err) {
throw err;
}
console.log('JSON output has been written to ' + fileName);
});
}