@ssttevee/xml-parser
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

Iterator-based XML Parser

A simple, zero-dependency XML parser that uses the iterator interface.

Basic Usage

for (const node of parse('<hello>world</hello>')) {
    console.log(node);
    // prints:
    //   { "type": "opentag", "opentag": { "name": "hello", "empty": false, "attributes": {} } }
    //   { "type": "text", "text": "world" }
    //   { "type": "closetag", "closetag": "hello" }
}
const res = await fetch('https://www.w3schools.com/xml/note.xml');
for await (const node of parseStream(res.body)) {
    console.log(node);
    // prints:
    //   { "type": "xmldecl", "xmldecl": { "version": "1.0", "encoding": "UTF-8" } }
    //   { "type": "text", "text": "\n" }
    //   { "type": "opentag", "opentag": { "name": "note", "empty": false, "attributes": {} } }
    //   { "type": "text", "text": "\n  " }
    //   { "type": "opentag", "opentag": { "name": "to", "empty": false, "attributes": {} } }
    //   { "type": "text", "text": "Tove" }
    //   { "type": "closetag", "closetag": "to" }
    //   { "type": "text", "text": "\n  " }
    //   { "type": "opentag", "opentag": { "name": "from", "empty": false, "attributes": {} } }
    //   { "type": "text", "text": "Jani" }
    //   { "type": "closetag", "closetag": "from" }
    //   { "type": "text", "text": "\n  " }
    //   { "type": "opentag", "opentag": { "name": "heading", "empty": false, "attributes": {} } }
    //   { "type": "text", "text": "Reminder" }
    //   { "type": "closetag", "closetag": "heading" }
    //   { "type": "text", "text": "\n  " }
    //   { "type": "opentag", "opentag": { "name": "body", "empty": false, "attributes": {} } }
    //   { "type": "text", "text": "Don't forget me this weekend!" }
    //   { "type": "closetag", "closetag": "body" }
    //   { "type": "text", "text": "\n" }
    //   { "type": "closetag", "closetag": "note" }
}

Advanced Usage

const p = new Parser();
p.write('<hello>wor');
for (const node of p) {
    console.log(node);
    // prints:
    //   { "type": "opentag", "opentag": { "name": "hello", "empty": false, "attributes": {} } }
}
p.write('ld</hello>asdf');
for (const node of p) {
    console.log(node);
    // prints:
    //   { "type": "text", "text": "world" }
    //   { "type": "closetag", "closetag": "hello" }
}
p.close();
for (const node of p) {
    console.log(node);
    // prints:
    //   { "type": "text", "text": "asdf" }
}

Importing

NodeJS

npm install @ssttevee/xml-parser
import { Parser, parse, parseStream } from '@ssttevee/xml-parser';

Deno

import { Parser, parse, parseStream } from 'https://raw.githubusercontent.com/ssttevee/js-iterator-based-xml-parser/trunk/mod.ts';

OR

import { Parser, parse, parseStream } from "https://unpkg.com/@ssttevee/xml-parser/mod.ts";

Readme

Keywords

none

Package Sidebar

Install

npm i @ssttevee/xml-parser

Weekly Downloads

1

Version

0.1.1

License

MIT

Unpacked Size

66.5 kB

Total Files

5

Last publish

Collaborators

  • ssttevee