Skip to content

Instantly share code, notes, and snippets.

@magicmonkey
Created April 3, 2011 19:02
Show Gist options
  • Save magicmonkey/900680 to your computer and use it in GitHub Desktop.
Save magicmonkey/900680 to your computer and use it in GitHub Desktop.
Demonstrates onEndDocument being called multiple times
var xml = require('./node-xml');
var hierachy = [];
var fs = require('fs');
var util = require('util');
var parser = new xml.SaxParser(function(cb) {
cb.onStartElementNS(function(elem, attrs, prefix, uri, namespaces) {
if (elem == 'row') {
var attrsLookup = {};
attrs.forEach(function(attr) {
attrsLookup[attr[0]] = attr[1];
});
hierachy.push(attrsLookup);
}
});
cb.onStartDocument(function() {
console.log("Started parsing");
});
var numFinishes = 0; // Weirdly, onEndDocument seems to be called several times
cb.onEndElementNS(function(elem, prefix, uri) {
if (elem == 'oxip') {
console.log("Finished document element " + elem);
}
});
cb.onEndDocument(function() {
numFinishes++;
console.log("Finished parsing with " + hierachy.length + " entries, finished " + numFinishes + " times");
});
});
var xmlcontents = fs.readFileSync('mkttmpl.xml').toString('utf8');
var chunkSize = 10;
for (var i=0; i<xmlcontents.length; i+=chunkSize) {
parser.parseString(xmlcontents.substr(i,chunkSize));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment