Created
August 13, 2020 01:31
-
-
Save fillano/513db0d68a8c9e4b5fe01f84fa078284 to your computer and use it in GitHub Desktop.
convert xml to simple json
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
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]'); | |
} |
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
{ | |
"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