Created
August 5, 2011 22:51
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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