-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathxml2json.js
More file actions
35 lines (26 loc) · 800 Bytes
/
xml2json.js
File metadata and controls
35 lines (26 loc) · 800 Bytes
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
/*jshint node:true */
"use strict";
var xm = require('xml-mapping'),
optimist = require('optimist'),
argv = optimist
.options('help',{alias:'h', describe: 'Show help'})
.options('caproot',{alias:'c',describe:'no root elem in JSON (if just one)'})
.usage('Reads standard input and writes to standard output.\nUsage : $0 [--help] [--caproot]')
.argv;
if ( argv.help ){
optimist.showHelp();
process.exit(0);
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
var xml = '';
process.stdin.on('data', function (chunk) {
xml = xml + chunk;
});
process.stdin.on('end', function() {
var json = xm.load(xml);
if(argv.caproot && Object.keys(json).length===1){
json = json[Object.keys(json)[0]];
}
console.log(JSON.stringify(json, null, '\t'));
});