Use sax as a Node.js Stream
mainFor streaming data (e.g., reading from a file or network), use sax.createStream(strict, options). This returns a standard Node.js readable/writable stream that supports .pipe().
Note on Error Handling: Because the stream is a proper EventEmitter, unhandled errors will throw. You must listen to the error event. To continue parsing after an error, you must clear the error on the internal parser and call resume().
// stream usage
// takes the same options as the parser
var saxStream = require('sax').createStream(strict, options)
saxStream.on('error', function (e) {
// unhandled errors will throw, since this is a proper node
// event emitter.
console.error('error!', e)
// clear the error
this._parser.error = null
this._parser.resume()
})
saxStream.on('opentag', function (node) {
// same object as above
})
// pipe is supported, and it's readable/writable
// same chunks coming in also go out.
fs.createReadStream('file.xml')
.pipe(saxStream)
.pipe(fs.createWriteStream('file-copy.xml'))