Last active
February 19, 2016 18:55
-
-
Save theraaz/613bb4c63939b4acd9fc to your computer and use it in GitHub Desktop.
process big files in node js, read xml files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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