Skip to content

Instantly share code, notes, and snippets.

@Vanuan
Created January 17, 2020 15:39
Show Gist options
  • Save Vanuan/c2ac2dbfa63c701c10ff633ee83ae218 to your computer and use it in GitHub Desktop.
Save Vanuan/c2ac2dbfa63c701c10ff633ee83ae218 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
let Parser = require('node-xml-stream');
let fs = require('fs');
if(process.argv.length != 3) {
console.log('usage:', process.argv[1], '[filename]');
process.exit(-1)
}
const filename = process.argv[2];
let parser = new Parser();
// <tag attr="attrValue">
parser.on('opentag', (name, attrs) => {
// name = 'tag'
// attrs = { attr: 'attrValue' }
});
// </tag>
parser.on('closetag', name => {
})
// <tag>TEXT</tag>
parser.on('text', text => {
// text = 'TEXT'
});
// <[[CDATA['data']]>
parser.on('cdata', cdata => {
// cdata = 'data'
});
// <?xml version="1.0"?>
parser.on('instruction', (name, attrs) => {
// name = 'xml'
// attrs = { version: '1.0' }
});
// Only stream-errors are emitted.
parser.on('error', err => {
// Handle a parsing error
console.log(err)
});
parser.on('finish', () => {
// Stream is completed
console.log('finished parsing')
});
// Pipe a stream to the parser
let stream = fs.createReadStream(filename);
stream.pipe(parser);
console.log('parsing...')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment