- Notifications
You must be signed in to change notification settings - Fork4
clipcrow/xmlp
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This project is an XML parser implemented for Deno as simply as possible. Currently it supports SAX style and Pull style.I'm thinking of using it only in applications that run on Deno. However, there is very little code that depends on Deno, so it's easy to make it available in Node (I don't).If you haven't programmed with Deno yet, give it a try. Very nice. SeeDeno official.
When using in SAX style, create an instance of the parser and register the listener in the same way as used in the EventEmitter of Node.The XML to be parsed is specified by Deno.Reader, UINT8 array, or a character string.
import{SAXParser}from'https://deno.land/x/xmlp/mod.ts';// create a SAX parser instanceconstparser=newSAXParser();// add SAX event handlersparser.on('start_prefix_mapping',(ns,uri)=>{console.log(`mapping start${ns}:${uri}`);}).on('text',(text,element)=>{if(element.qName==='m:comment'){console.log(`${element.attributes[0].value}:${text}`);}});// run parser, input source is Deno.Reader or Uint8Array or stringconstreader=awaitDeno.open('parser_test.xml');awaitparser.parse(reader,'shift_jis');// 2nd param is optional, specifies your XML encoding.
SAX event listener register definitions are below.
interfaceSAXEvent{start_document:()=>void;processing_instruction:(procInst:string)=>void;sgml_declaration:(sgmlDecl:string)=>void;text:(text:string,element:ElementInfo,cdata:boolean)=>void;doctype:(doctype:string)=>void;start_prefix_mapping:(ns:string,uri:string)=>void;start_element:(element:ElementInfo)=>void;comment:(comment:string)=>void;end_element:(element:ElementInfo)=>void;end_prefix_mapping:(ns:string,uri:string)=>void;end_document:()=>void;error:(error:XMLParseError)=>void;}classSAXParser{on<KextendskeyofSAXEvent>(event:K,listener:SAXEvent[K]): this{}}
You can use "SAXParser" on Deno's stream i/o because this is a simple "UnderlyingSink" impl.See theparser.ts / SAXParser#parse() -> #getWriter() -> getStream() -> write() chain.
I think it's more interesting to write the Pull style than the SAX. This Pull parser is implemented using the ES6 Generator / Iterator mechanism. However, the basic implementation is shared with that of the SAX parser.
Currently the Pull parser supports Uint8 arrays and strings, not Deno.Reader.
import{PullParser}from'https://deno.land/x/xmlp/mod.ts';// create a pull parser instanceconstparser=newPullParser();// create an ES6 generatorconstuint8Array=awaitDeno.readFile('parser_test.xml');constevents=parser.parse(uint8Array/*, encoding */);// pull events, using iteratorconstevent=events.next();if(event.value){console.log(event.value.name);}// using spread operatorconsole.log([...events].filter(({ name})=>{returnname==='text';}).map(({ text, cdata})=>{returncdata ?`<![CDATA[${text}]]>` :text;}));
Will be realized eventually.
Usually all you need see is mod.ts, SAXParser class or PullParser class.
https://doc.deno.land/https/deno.land/x/xmlp/mod.ts
The basic logic of this XML parser was obtained by reading the source code ofsax-js. Thanks.
The scripts and documentation in this project are released under theMIT License
About
XML Parser for Deno