具有while循环的NodeJS结束事件

时间:2016-02-06 18:50:06

标签: javascript node.js sax

我尝试使用sax迭代这个大的xml文件,这里是代码:

'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);

console.log会在 43 周围记录Done reading: 5,如果我注释掉while循环,它只会控制Done reading: 5 一次!< / strong>,我做错了什么?,这是一个错误吗?

2 个答案:

答案 0 :(得分:2)

因此,您希望在需要继续数据管道时暂停可读流。这就是shutdown函数应该超出管道范围的原因,您可以在readable.pause函数内将其暂停为done

'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 => {
    // I only need nodes named 'row'
    if(node.name === 'row' && rowsAdded < 5) {
        rowsAdded++;
        // If the name is 'row' and `attribute` prop exists, push it. 
        if(node.attributes) rows.push(node.attributes);
    }
    if(rowsAdded === 5)
        done();
})
.on('error', () => {
})
.on('end', () => {
    console.log('Done reading:', rowsAdded);
});

var readable = fs.createReadStream(feedFile, options)
readable.pipe(saxStream);

function done(){
    // this should stop reading part.
    readable.pause();
    while(rowsAdded--) {
        // do you processing here
    }
}

答案 1 :(得分:2)

当你从saxStream.on(&#39; opentag&#39;)返回时,这意味着你已经完成了对该标记的处理,但是解析器会继续,直到它完成整个xml。