javascript - NodeJS end event with while loop -


i try iterate big xml file using sax, here 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();     }     // need nodes named 'row'     if(node.name === 'row') {         rowsadded++;         // if name 'row' , `attribute` prop exists, push it.          if(node.attributes) rows.push(node.attributes);     } }) .on('error', () => { }) .on('end', () => {     console.log('done reading:', rowsadded);     // if remove while loop above console called once     while(rowsadded--) {      } }); fs.createreadstream(feedfile, options).pipe(saxstream); 

the console.log log done reading: 5 around 43 times, if comment out while loop, console done reading: 5 once!, doing wrong?, bug?

when return saxstream.on('opentag') means finished working on tag, parser continues until finishes whole xml.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -