- Notifications
You must be signed in to change notification settings - Fork0
NodeJS Streams wrapper around SaxesParser (package saxes) with single event stream.
License
NotificationsYou must be signed in to change notification settings
SmallhillCZ/saxes-stream
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
NodeJS Streams wrapper aroundSaxesParser (npm package saxes)
- Single event stream
- Pipable to other streams
- TypeScript declarations
Any string/Buffer Saxes would accept.
import{SaxesTag}from"saxes";exporttypeSaxesStreamChunk={event:"opentag",path:string,tag:SaxesTag}|{event:"text",path:string,text:string}|{event:"closetag",path:string,tag:SaxesTag};
Standard NodeJS Streams events.
import{SaxesOptions,SaxesStream,SaxesStreamChunk}from"saxes-stream";import{Transform,TransformOptions,TransformCallback}from"stream";importrequestfrom"request";importfsfrom"fs";importstringifyfrom"csv-stringify";/* Source HTTP XML stream */consthttpStream=request(url);/* SaxesStream XML parser */constconstsaxesOptions:SaxesOptions={// ...}conststreamOptions:TransformOptions={// ...}constparser=newSaxesStream(saxesOptions,streamOptions);/* Custom Transform Stream to pick the data from XML */classBookTransformextendsTransform{item:any={};// Stream settingreadableObjectMode=true;writableObjectMode=true;_transform(chunk:SaxesStreamChunk,encoding:string,callback:TransformCallback){/* Save parts by matching path */if(chunk.event==="text"){switch(chunk.path){case".books.book.title":this.item["title"]=chunk.text;break;case".books.book.content":this.item["content"]=chunk.text;break;}}/* On ending </book> tag emit the completed book object as data (or piped to a next stream) */if(chunk.event==="closetag"){switch(chunk.path){case".books.book":this.push(this.item);this.item={};break;}}callback();}}constbookTransform=newBookTransform();/* JSON -> CSV Transform Stream */constcsv=stringify({delimiter:','});/* File Write Stream */constfile=fs.createWriteStream('./books.csv');/* PIPE ALL THINGS TOGETHER */httpStream.pipe(parser).pipe(bookTransform).pipe(csv).pipe(file);