0

I have a large xml which is a combination of xml documents. I'm trying to use a nodejs xml splitter and respond back with the number of documents i have found.

My code looks something like this. I'm looking to get the number of documents outside the function(in the last line). Is there something I can do to achieve this?

var XmlSplit = require('./xmlsplitter.js')
const fs = require('fs')

var xmlsplit = new XmlSplit()
var no_of_docs = 0
var inputStream = fs.createReadStream('./files/input/test.xml') 
inputStream.pipe(xmlsplit).on('data', function(data,callback) {
    var xmlDocument = data.toString();
    no_of_docs = no_of_docs + 1;
})
inputStream.pipe(xmlsplit).on('end', function(){
    console.log('Stream ended');
    console.log(no_of_docs);   <-- This prints the correct value but the value is lost as soon as we exit this.
});
console.log("This is outside the function " + no_of_docs);  <-- I need the value here.
Anupam Chand
  • 2,209
  • 1
  • 5
  • 14

0 Answers0