Skip to content

Instantly share code, notes, and snippets.

@theraaz
Last active February 19, 2016 18:55
Show Gist options
  • Select an option

  • Save theraaz/613bb4c63939b4acd9fc to your computer and use it in GitHub Desktop.

Select an option

Save theraaz/613bb4c63939b4acd9fc to your computer and use it in GitHub Desktop.
process big files in node js, read xml files
/**
* full article at
* http://theraaz.com/javascript/how-to-process-big-files-in-node-js
*/
var bigFileUrl = "http://www.site-containing-big-data/api";
request.get(bigFileUrl)
.on('error', function(errReq) {
console.log('error while reading from big site : ', errReq);
// do something here if you want to throw an exception or something else
}).on('end', function(){
/*
* once all the data received and saved successfully in bigfile.xml
* request will fire this event.
*/
console.log('got from big site, now processing');
// now read that file with xml-stream
var XmlStream = require('xml-stream') ;
var stream=fs.createReadStream('bigfile.xml');
xml = new XmlStream(stream);
// item is the node that contains data. check sample xml below
xml.on('endElement: item', function(item) {
// it will read all the objects one at a time and here you can process that object,
// if a lot of processing required, you can call xml.pause() and xml.resume() method available in xml-stream package
});
xml.on('end', function(){
// when processing finished for all objects/items in that file
});
});
})
.pipe(fs.createWriteStream('bigfile.xml'));
// this pipe method will stream data to bigfile.xml
/**
* <items>
* <item>
* <name>alex</name>
* <age>23</age>
* </item>
* <item>
* <name>john</name>
* <age>17</age>
* </item>
* <item>
* <name>smona</name>
* <age>12</age>
* </item>
* <item>
* <name>smith</name>
* <age>34</age>
* </item>
* </items>
* /
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment