Skip to content

Instantly share code, notes, and snippets.

@companje
Last active July 15, 2019 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save companje/488ff20a81829ff5d354f6864ad3799f to your computer and use it in GitHub Desktop.
Save companje/488ff20a81829ff5d354f6864ad3799f to your computer and use it in GitHub Desktop.
Parse multiple Export XML file and output fields
#!/usr/bin/env node
//usage: find ~/Documents/Export -name "*.txt" -exec node id-guid-aet.js {} ';' > id-guid-aet.csv
//or sorted: find ~/Documents/Export -name "*.txt" | sort | while IFS= read -r filename; do node id-guid.js "$filename"; done > id-guid-aet.csv
const fs = require('fs');
const node_xml_stream = require('node-xml-stream');
const parser = new node_xml_stream();
const filename = process.argv[2];
var tag,item;
if (!filename) return console.error("usage: ./index.js input_xml_file");
if (!fs.existsSync(filename)) return console.error("File not foud: " + filename);
console.error(filename); //not realy error but for feedback during run
let stream = fs.createReadStream(filename, 'UTF-8');
stream.pipe(parser);
parser.on('opentag', function(name, attrs) {
tag = name;
if (name=="AHD") {
//save first
if (item) console.log(item["id"]+","+item["guid"]+","+item["aet"]+","+item["ahd_id"]);
//prepare for new item
item = {};
}
});
parser.on('text', function(text) {
item[tag.toLowerCase()] = text;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment