Created
October 6, 2018 19:33
-
-
Save kappuccino/28b2296f74c68c9fdd5c0363f83e0fd5 to your computer and use it in GitHub Desktop.
Read large XML
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
const fs = require('fs') | |
const lineReader = require('readline').createInterface({ | |
input: fs.createReadStream('061018-formated.xml') | |
}); | |
let item = [] | |
let inside = false | |
let index = 1 | |
lineReader.on('line', async function (line) { | |
if (line.trim().substr(0, 4) === '<ROW') start() | |
if (inside) push(line) | |
if (line.trim().substr(0, 5) === '</ROW') await end() | |
}) | |
function start() { | |
inside = true | |
} | |
async function end() { | |
lineReader.pause() | |
inside = false | |
const row = item.join('\n') | |
await saveToFile(row) | |
lineReader.resume() | |
index++ | |
} | |
function push(line) { | |
item.push(line) | |
} | |
async function saveToFile(xml){ | |
const file = `${__dirname}/profiles/${index}.xml` | |
return new Promise((resolve, reject) => { | |
fs.writeFile(file, xml, (err) => { | |
if (err) return reject(err) | |
//console.log(`The file ${file} has been saved!`) | |
resolve() | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment