Skip to content

Instantly share code, notes, and snippets.

@fillano
Created August 13, 2020 01:31
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 fillano/513db0d68a8c9e4b5fe01f84fa078284 to your computer and use it in GitHub Desktop.
Save fillano/513db0d68a8c9e4b5fe01f84fa078284 to your computer and use it in GitHub Desktop.
convert xml to simple json
const xmlrun = require('xmlrun');
const fs = require('fs');
const args = require('minimist')(process.argv.slice(2));
main(args);
function main(args) {
if(args._.length > 0) {
fs.readFile(args._[0], 'utf8', (err, data) => {
if(!!err) return console.log(err);
let runner = xmlrun(data);
runner.setRunner('default', function(target) {
let result = {
type: xmlrun.utils.getNodeTypeDesc(target.type),
tagName: target.tag,
attributes: target.attr,
value: target.val,
children: []
}
if(!!target.child) {
result.children = target.child.reduce((acc, cur) => {
acc.push(cur.run());
return acc;
}, []);
}
return result;
})
let obj = runner.run();
console.log(JSON.stringify(obj, null, 2));
});
} else {
help();
}
}
function help() {
console.log('[usage] node amlrun [aml file path]');
}
{
"name": "amlrun",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"minimist": "^1.2.5",
"xmlrun": "fillano/xmlrun#9b42def77cd5a80b1143506f4ea590e88cd52bf0"
},
"author": "",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment