parseXmlStream
import { parseXmlStream } from "jsr:@ende/xml";function
async function parseXmlStream(source: AsyncIterable<string>, callbacks: XmlEventCallbacks, options: ParseStreamOptions = [UNSUPPORTED]): Promise<void>Parse XML from a stream with maximum throughput using direct callbacks.
This function provides the highest performance streaming XML parsing by invoking callbacks directly without creating intermediate event objects. Use this when you need maximum throughput and are comfortable with the callback-based API.
source: AsyncIterable<string>The async iterable of XML string chunks to parse.
callbacks: XmlEventCallbacksThe event callbacks invoked during parsing.
options: ParseStreamOptions = [UNSUPPORTED]Options for configuring the parser.
Returns
A promise that resolves when parsing is complete.
Example: Collecting data from elements
import { parseXmlStream } from "@ende/xml/parse-stream";
import { assertEquals } from "@ende/assert";
const xml = `<root><item id="1">First</item><item id="2">Second</item></root>`;
const stream = ReadableStream.from([xml]);
const items: string[] = [];
let currentText = "";
let inItem = false;
await parseXmlStream(stream, {
onStartElement(name) {
if (name === "item") {
inItem = true;
currentText = "";
}
},
onText(text) {
if (inItem) currentText += text;
},
onEndElement(name) {
if (name === "item") {
items.push(currentText);
inItem = false;
}
},
});
assertEquals(items, ["First", "Second"]);