0

嘿,我正在使用 htmlparser2 来解析 xml。以下是我的代码

var htmlparser = require('htmlparser2');
var fs = require('fs');
var sitemapUrls = [];
var parser = new htmlparser.Parser({
    ontext: function(text){
        if(text.match(/foo/)){
            sitemapUrls.push(text);
        }
    }
 });
 fs.createReadStream('./sitemap-index.xml').pipe(parser).on('end',function(){
   console.log(sitemapUrls.length);
 });

我无法找到 htmlparser2 是否有任何事件告诉我们解析已完成。

我想打印 sitemapUrls 数组的长度。

提前致谢

4

1 回答 1

1

我在处理程序中找到了答案,我需要添加事件“onend”,一旦解析完成,该事件将被调用

var parser = new htmlparser.Parser({
    ontext: function(text){
        if(text.match(/myntra/)){
            sitemapUrls.push(text);
        }
    },
    onend: function(){
        console.log(sitemapUrls.length);
    }
});
于 2014-01-22T08:35:26.077 回答