Created
May 11, 2017 14:47
-
-
Save JamesKyburz/d1c8da834e95f8ce7b5cf43fc32e6a2e to your computer and use it in GitHub Desktop.
xml streams sax
This file contains 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 sax = require('sax') | |
const strict = false | |
const duplex = require('duplexer2') | |
const stream = require('stream') | |
module.exports = (itemPath) => { | |
const parser = sax.parser(strict, { lowercase: true, trim: true, normalize: true }) | |
const readable = new stream.Readable({ objectMode: true }) | |
const writable = new stream.Writable({ objectMode: true }) | |
readable._read = (f) => f | |
let root = '' | |
let item | |
let tag | |
parser.ontext = (text) => { | |
const value = text.trim() | |
if (item) { | |
if (!item[tag]) item[tag] = '' | |
item[tag] += value | |
if (tag === 'category') { | |
console.log('text', text) | |
console.log('value', value) | |
console.log('item[tag]', item[tag]) | |
} | |
} | |
} | |
parser.onopentag = (node) => { | |
if (root) root += '.' | |
root += node.name | |
if (root === itemPath) { | |
item = {} | |
} else { | |
tag = node.name | |
} | |
} | |
parser.onclosetag = (node) => { | |
if (root === itemPath) { | |
if (item && Object.keys(item).length) readable.push(item) | |
} | |
root = root.split('.').slice(0, -1).join('.') | |
} | |
parser.onerror = (err) => { | |
reader.emit('error', err) | |
}) | |
writable._write = (chunk, encoding, cb) => { | |
console.log('ENCODING', encoding) | |
parser.write(chunk) | |
cb() | |
} | |
writable.once('finish', () => readable.push(null)) | |
return duplex({ objectMode: true }, writable, readable) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment