assistunion / xml-stream

XML stream parser based on Expat. Made for Node.
MIT License
318 stars 110 forks source link

xml-stream isn't detecting events in short xml documents #55

Closed ClaysonIO closed 8 years ago

ClaysonIO commented 9 years ago

I'm using xml-stream inside a nodejs worker, retrieving xml using the request package. For some reason, I can pull data from moderately large files, but not from short ones. On short documents, the only event to be called is 'end'.

I've verified that the documents are coming through, but haven't been able to figure out why the events aren't firing.

request.get(options)
.on('error', function(err){
  console.log(err);
  job.fail();
  cb();
})
.on("response", function(response, body){
  var allEvents = [];
  response.setEncoding('utf8');

  var xml = new xmlStream(response);

  xml.collect('r25:item');
  xml.collect('r25:id');
  xml.collect('r25:name');

  xml.on('endElement: r25:item', function(data){
    console.log("Found Event");
    var thisEvent = 0;
    allEvents.push(thisEvent);
  })

  xml.on('end', function(data){
    console.log("EventCount", allEvents.length);
    console.log();
    job.done();
    cb();
  });
})
nkbt commented 8 years ago

Can you please doublecheck that endElement: r25:item event is not emitted AFTER end in your case (just put a decent timeout before running callback)?

In my case what I found - after end I can still receive several endElement events.

nkbt commented 8 years ago

The solution I came up with is to listen to endElement of your root XML element instead of stream end:

something like:

xml.on('endElement: data_container', function(data){
    console.log("EventCount", allEvents.length);
    console.log();
    job.done();
    cb();
  })

and remove xml.on('end') listener

ClaysonIO commented 8 years ago

Apologies for the delay -- I don't have access to the API that was causing this issue any more (short term project), so I can't do additional trouble shooting. The next time I work on a similar system, I'll see about getting more data.

Thanks.