0.0.6 • Published 7 years ago
xml-tag-stream v0.0.6
Xml-tag-stream
A simple module for streaming xml content split by a given tag name
Basic usage
const XmlTagStream = require('xml-tag-stream');
const stream = require('stream');
const readable = new stream.Readable;
readable
.pipe(new XmlTagStream('abc'))
.on('data', console.log.bind(console, '-'))
.on('end', console.log.bind(console, '-', 'streaming ended'))
readable.push(`
<root>
<abc>
123
</abc>
<abc x="123">
<def y="456">
ghi
</def>
</abc>
</root>
`);
readable.push(null);
The following output will get printed:
- <abc>
123
</abc>
- <abc x="123">
<def y="456">
ghi
</def>
</abc>
- streaming ended
The module can be easily combined with some other modules like for example with xml2js and through2
const fs = require('fs');
const XmlTagStream = require('xml-tag-stream');
const through2 = require('through2');
const xmlParser = require('xml2js').Parser();
fs.createReadStream('./some.xml')
.pipe(new XmlTagStream('abc'))
.pipe(through2.obj((tag, enc, cb) => xmlParser.parseString(tag, cb)))
.on('data', console.log.bind(console, '-\n'))
.on('end', console.log.bind(console, '-\n', 'streaming ended'))