mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 03:45:59 +00:00
xml plugin initial import
This commit is contained in:
42
plugins/dbgate-plugin-xml/src/backend/reader.js
Normal file
42
plugins/dbgate-plugin-xml/src/backend/reader.js
Normal file
@@ -0,0 +1,42 @@
|
||||
const fs = require('fs');
|
||||
const stream = require('stream');
|
||||
const NodeXmlStream = require('node-xml-stream');
|
||||
|
||||
class ParseStream extends stream.Transform {
|
||||
constructor({ elementName }) {
|
||||
super({ objectMode: true });
|
||||
this.rowsWritten = 0;
|
||||
this.parser = new NodeXmlStream();
|
||||
this.stack = [];
|
||||
this.parser.on('opentag', (name, attrs) => {
|
||||
this.stack.push({ name, attrs, nodes: {} });
|
||||
});
|
||||
this.parser.on('text', (text) => {
|
||||
if (this.stack.length >= 2) {
|
||||
this.stack[this.stack.length - 2].nodes[this.stack[this.stack.length - 1].name] = text;
|
||||
}
|
||||
});
|
||||
this.parser.on('closetag', (name, attrs) => {
|
||||
if (name == elementName) {
|
||||
this.rowsWritten += 1;
|
||||
this.push({ ...this.stack[this.stack.length - 1].attrs, ...this.stack[this.stack.length - 1].nodes });
|
||||
}
|
||||
this.stack.splice(-1);
|
||||
});
|
||||
}
|
||||
_transform(chunk, encoding, done) {
|
||||
this.parser.write(chunk);
|
||||
done();
|
||||
}
|
||||
}
|
||||
|
||||
async function reader({ fileName, encoding = 'utf-8', elementName }) {
|
||||
console.log(`Reading file ${fileName}`);
|
||||
|
||||
const fileStream = fs.createReadStream(fileName, encoding);
|
||||
const parser = new ParseStream({ elementName });
|
||||
fileStream.pipe(parser);
|
||||
return parser;
|
||||
}
|
||||
|
||||
module.exports = reader;
|
||||
Reference in New Issue
Block a user