parseXmlRecordsFromBytes

import { parseXmlRecordsFromBytes } from "jsr:@ende/xml";

function

function parseXmlRecordsFromBytes<T>(source: AsyncIterable<Uint8Array>, createCallbacks: (emit: (record: T) => void) => XmlEventCallbacks, options: ParseStreamOptions = [UNSUPPORTED]): AsyncGenerator<T>

Parses an async iterable of XML byte chunks and yields records assembled inside SAX-style event callbacks.

This is a convenience wrapper around parseXmlRecords that handles text decoding via a streaming TextDecoder. For pre-decoded text streams, use parseXmlRecords directly.

source: AsyncIterable<Uint8Array>

An async iterable of XML byte chunks (e.g. response.body from a fetch call).

createCallbacks: (emit: (record: T) => void) => XmlEventCallbacks

Factory invoked once with an emit function; returns the SAX-style callbacks that build records and call emit per completed record.

options: ParseStreamOptions = [UNSUPPORTED]

Parser options forwarded to the underlying tokenizer/parser.

Returns

An async generator that yields records as the document is parsed.

Example: Parse items from a byte stream

import { parseXmlRecordsFromBytes } from "@ende/xml/parse-records";
import { assertEquals } from "@ende/assert";

const xml = new TextEncoder().encode(
  `<feed><item>First</item><item>Second</item></feed>`,
);

const titles: string[] = [];
for await (
  const title of parseXmlRecordsFromBytes<string>(
    ReadableStream.from([xml]),
    (emit) => {
      let inside = false;
      let text = "";
      return {
        onStartElement(name) {
          if (name === "item") {
            inside = true;
            text = "";
          }
        },
        onText(t) {
          if (inside) text += t;
        },
        onEndElement(name) {
          if (name === "item") {
            emit(text);
            inside = false;
          }
        },
      };
    },
  )
) {
  titles.push(title);
}

assertEquals(titles, ["First", "Second"]);

Source