I try to iterate this big xml file using sax, here is the code:
'use strict';
const fs = require('fs');
const sax = require('sax');
let rowsAdded = 0;
let rows = [];
let options = {
encoding: 'utf8',
mode: 0o444
};
let strict = true,
feedFile = 'Comments.xml',
saxStream = sax.createStream(strict);
saxStream.on('opentag', node => {
if(rowsAdded === 5) {
return saxStream.end();
}
// I only need nodes named 'row'
if(node.name === 'row') {
rowsAdded++;
// If the name is 'row' and `attribute` prop exists, push it.
if(node.attributes) rows.push(node.attributes);
}
})
.on('error', () => {
})
.on('end', () => {
console.log('Done reading:', rowsAdded);
// If you remove this while loop the above console will called only once
while(rowsAdded--) {
}
});
fs.createReadStream(feedFile, options).pipe(saxStream);
The console.log
will log Done reading: 5
around 43 times, if I comment out the while loop, it will only console Done reading: 5
once!, am I doing something wrong?, is it a bug?
When you return from saxStream.on('opentag') it means that you finished working on that tag, but the parser continues until it finishes the whole xml.