0.5.2 • Published 4 months ago

stream-xml v0.5.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

stream-xml

Streaming XML parser using callbacks for handling individual tags.

Installing with Yarn or NPM

yarn add stream-xml

or

npm i stream-xml

Installing in Deno

import { Parser } from "https://deno.land/x/stream_xml/lib/parser.ts";

Usage

Example

Parsing a Stream

import { createReadStream } from "node:fs";
import { StreamParser } from "stream-xml";

const streamParser = new StreamParser();
streamParser.parser.onElement("myTag", () => {
  console.log("Encountered my tag!");
  // get attributes using: parser.attributes()
});
streamParser.parser.onTextNode(() => {
  console.log("Encountered a text node");
  // get the content using: parser.textContent()
});

const file = createReadStream("data.xml");
file.pipe(streamParser);

streamParser.on("finish", () => console.log("Done 🎉"));

Parsing an Entire File

import { readFileSync } from "node:fs";
import { Parser } from "stream-xml";

const parser = new Parser();

parser.onElement("myTag", () => {
  console.log("Encountered my tag!");
  // get attributes using: parser.attributes()
});
parser.onTextNode(() => {
  console.log("Encountered a text node");
  // get the content using: parser.textContent()
});

const file = readFileSync("data.xml");
parser.parse(file);

Options

You can pass these in an object to the Parser constructor.

bufferSize

Type: number

Default: 128 * 1024

The size of the internal buffer. Should be at least double that of the buffers that get pushed into the stream.

encoding

Type: BufferEncoding

Default: utf-8

Encoding that is used when converting parts of the XML document, e.g. attributes or text nodes, into strings.

0.5.2

4 months ago

0.5.1

11 months ago

0.5.0

11 months ago

0.4.0

11 months ago

0.3.0

11 months ago

0.2.1

11 months ago

0.2.0

11 months ago

0.1.0

11 months ago