stream-xml
TypeScript icon, indicating that this package has built-in type declarations

0.5.2 • Public • Published

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.

Readme

Keywords

none

Package Sidebar

Install

npm i stream-xml

Weekly Downloads

329

Version

0.5.2

License

MIT

Unpacked Size

165 kB

Total Files

36

Last publish

Collaborators

  • leolabs