Question

Hey I am using htmlparser2 to parse the xml. following is my code

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

I am not able find whether do we have any event for htmlparser2 which tells us the parsing is completed.

I want to print the length of sitemapUrls array .

Thanks In Advance

Was it helpful?

Solution

I found the answer in handler I need to add event 'onend' which will be called once parsing completes

var parser = new htmlparser.Parser({
    ontext: function(text){
        if(text.match(/myntra/)){
            sitemapUrls.push(text);
        }
    },
    onend: function(){
        console.log(sitemapUrls.length);
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top