Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

NodeJS Streams wrapper around SaxesParser (package saxes) with single event stream.

License

NotificationsYou must be signed in to change notification settings

SmallhillCZ/saxes-stream

Repository files navigation

NodeJS Streams wrapper aroundSaxesParser (npm package saxes)

  • Single event stream
  • Pipable to other streams
  • TypeScript declarations

API

Input

Any string/Buffer Saxes would accept.

Output chunk format:

import{SaxesTag}from"saxes";exporttypeSaxesStreamChunk={event:"opentag",path:string,tag:SaxesTag}|{event:"text",path:string,text:string}|{event:"closetag",path:string,tag:SaxesTag};

Events

Standard NodeJS Streams events.

Example 1 - Get huge online XML of books and stream parse it into CSV

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);

About

NodeJS Streams wrapper around SaxesParser (package saxes) with single event stream.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp