Last active
July 15, 2019 20:17
-
-
Save companje/488ff20a81829ff5d354f6864ad3799f to your computer and use it in GitHub Desktop.
Parse multiple Export XML file and output fields
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
#!/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