Skip to content

Instantly share code, notes, and snippets.

@sfoster
Created August 5, 2011 22:51
Show Gist options
  • Save sfoster/1128716 to your computer and use it in GitHub Desktop.
Save sfoster/1128716 to your computer and use it in GitHub Desktop.
hooking up xml2json to convert XML piped in via stdin to JSON on stdout
// hooking up xml2json to convert XML piped in via stdin to JSON on stdout
// e.g.:
// curl http://www.gutenberg.org/feeds/today.rss | node index.js > gutenberg.json
var x2j = require("xml2json");
process.stdin.resume();
process.stdin.setEncoding('utf8');
var xml = '';
process.stdin.on('data', function (chunk) {
xml += chunk;
});
process.stdin.on('end', function () {
if(xml) {
var json = x2j.toJson(xml);
process.stdout.write(json);
} else {
process.stdout.write(json);
console.log("no input, pipe it in on stdin \n"
+ "e.g $ cat somefile.xml | node xml2json.js");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment