# stream-xml

> A fast streaming XML parser

Latest version **0.6.0** (published 2025-01-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install stream-xml
pnpm add stream-xml
yarn add stream-xml
bun add stream-xml
```

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.0 |
| Published | 2025-01-19 |
| First published | 2023-06-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 163.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Maintainers | leolabs |

## Links

- npm: https://www.npmjs.com/package/stream-xml
- Repository: https://github.com/leolabs/stream-xml
- npm.io page: https://npm.io/package/stream-xml

## Dependencies (1)

- [entities](https://npm.io/package/entities.md) ^4.5.0

## Recent versions

- 0.6.0 (latest) — 2025-01-19
- 0.5.2 — 2023-12-29
- 0.5.1 — 2023-06-05
- 0.5.0 — 2023-06-05
- 0.4.0 — 2023-06-04
- 0.3.0 — 2023-06-04
- 0.2.1 — 2023-06-04
- 0.2.0 — 2023-06-04
- 0.1.0 — 2023-06-03

## README

# `stream-xml`

Streaming XML parser using callbacks for handling individual tags.

## Installing with Yarn or NPM

```sh
yarn add stream-xml
```

or

```sh
npm i stream-xml
```

## Installing in Deno

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

## Usage

### Example

#### Parsing a Stream

```js
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

```js
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.

---
_Source: https://npm.io/package/stream-xml · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
