stream-xml
v0.5.2
Published
A fast streaming XML parser
Downloads
2,837
Readme
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.