Socket
Socket
Sign inDemoInstall

stream-xml

Package Overview
Dependencies
1
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    stream-xml

A fast streaming XML parser


Version published
Weekly downloads
172
increased by117.72%
Maintainers
1
Install size
565 kB
Created
Weekly downloads
 

Readme

Source

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.

FAQs

Last updated on 29 Dec 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc