Last active
November 6, 2017 20:39
-
-
Save maxant/f733abf9506f4ea5a8fdcde44734d7c2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// npm install xml2js | |
// compile using "./node_modules/.bin/tsc read_xml_out_csv.ts -w" | |
// run using node read_xml_out_csv.js | |
const parseString: any = require('xml2js').parseString; | |
import * as fs from "fs"; | |
const model = {}; // dictionary of dictionaries with filename -> servlet -> parameter name -> parameter value | |
const params = {}; // all params that are found (mapped to undefined) | |
const start = Date.now(); | |
// parse files and collect all data | |
fs.readdir('.', function(err: any, files: any) { | |
files = files.filter((file) => {return file.endsWith(".xml"); }); | |
const allResults = []; | |
files.forEach((file) => { | |
const p = handleFile(file); | |
allResults.push(p); | |
}); | |
//this was NOT waiting until everything was done. "await" is a little crap it appears. | |
//so lets work with promises as well | |
Promise.all(allResults).then((res) => { | |
output(files); | |
}); | |
}); | |
async function handleFile(file){ | |
console.log("parsing file " + file); | |
model[file] = {}; | |
const xml = fs.readFileSync(file); | |
const result = await parseXml(xml); | |
const servlets = result.config.servlet; | |
servlets.forEach((servlet) => { | |
let servletName = servlet.name[0]; | |
model[file][servletName] = {}; | |
servlet["param-name"].forEach((name, i) => { | |
params[name] = undefined; | |
model[file][servletName][name] = servlet["param-value"][i]; | |
}); | |
}); | |
//async functions implicitly return a promise ;-) | |
} | |
function parseXml(xml): Promise<any> { | |
return new Promise((resolve, reject) => { | |
parseString(xml, (err, result) => { | |
if(err) reject(err); | |
else resolve(result); | |
}); | |
}); | |
} | |
function output(files){ | |
// build header | |
let lines = "filename,servlet name,"; | |
for(let param in params){ | |
lines += param + ","; | |
} | |
lines += "\n"; | |
// build rows | |
files.forEach((file) => { | |
for(let servlet in model[file]) { | |
lines += file + "," + servlet + ","; | |
for(let param in params){ | |
if(model[file][servlet][param]){ | |
lines += model[file][servlet][param]; | |
} | |
lines += "," | |
} | |
lines += "\n" | |
} | |
}); | |
// write output | |
fs.writeFileSync('output.csv', lines); | |
console.log("output written to output.csv in " + (Date.now()-start) + "ms"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment