@simple-api-stream/xml
Streaming XML (SAX = Simple API for XML) parser, AST/DOM converters, serializer, and optional JSON (SAJ) bridge built on the Web Streams API.
Installation
npm install @simple-api-stream/xml
Requires Node.js >= 24 or any runtime supporting Web Streams and async iteration of ReadableStream.
Features
| Export |
Type |
Input |
Output |
XMLTextToSAXTransformStream |
TransformStream |
XML text (string chunks) |
SAXEventInterface objects |
XMLTextToSAXEventWritableStream |
WritableStream |
XML text (string chunks) |
Calls a SAXHandler |
SAXToObjectTransformStream<T> |
TransformStream |
SAX events |
Lightweight plain-object XML AST (XMLDocumentNode) or mapped T |
ObjectToSAXTransformStream<T> |
TransformStream |
Lightweight XML AST (XMLNode) or mapped T |
SAX events |
SAXToDOMTransformStream<T> |
TransformStream |
SAX events |
DOM Document or DocumentFragment |
DOMToSAXTransformStream |
TransformStream |
DOM-like nodes (DOMNodeLike) |
SAX events |
SAXToXMLTextTransformStream |
TransformStream |
SAX events |
XML text |
ResolveToSAXReadableStream |
ReadableStream |
Method calls (SAXResolver) |
SAX events |
parseXMLResponse<T> |
Helper function |
Fetch Response |
ReadableStream<T> (XMLDocumentNode or mapped T) |
SAXToSAJTransformStream (@simple-api-stream/xml/json) |
TransformStream |
SAX events |
SAJ events (lossless XMLDocumentNode AST JSON, O(depth) streaming) |
SAJToSAXTransformStream (@simple-api-stream/xml/json) |
TransformStream |
SAJ events (XMLNode AST JSON) |
SAX events |
Usage
import { parseXMLResponse } from "@simple-api-stream/xml";
const response = await fetch("https://example.com/feed.xml");
for await (const doc of parseXMLResponse(response)) {
console.log("Parsed XML:", doc);
}
Or compose the underlying Web Streams directly:
import { XMLTextToSAXTransformStream } from "@simple-api-stream/xml";
const events = xmlTextStream.pipeThrough(new XMLTextToSAXTransformStream());
for await (const event of events) {
if (event.name === "startElement")
console.log(event.tagName, event.attrs);
}
Converting XML to JSON (@simple-api-stream/xml/json)
import { XMLTextToSAXTransformStream } from "@simple-api-stream/xml";
import { SAXToSAJTransformStream } from "@simple-api-stream/xml/json";
import { SAJToJSONTextTransformStream } from "@simple-api-stream/json";
const jsonText = xmlTextStream
.pipeThrough(new XMLTextToSAXTransformStream())
.pipeThrough(new SAXToSAJTransformStream())
.pipeThrough(new SAJToJSONTextTransformStream({ indent: 2 }));
SAX Events
name |
Extra fields |
startDocument |
kind: "xml" |
endDocument |
– |
startElement |
tagName, attrs: Record<string, string>, selfClosing: boolean |
endElement |
tagName |
text |
text (numeric references and XML/HTML5 named entities are unescaped) |
cdata |
cdata |
comment |
comment (preserved verbatim without entity unescaping) |
doctype |
root, dtdType?: "PUBLIC" \| "SYSTEM", identifier?, uri?, declarations?: string[] |
processingInstruction |
target, data; <?xml …?> additionally has version, encoding, standalone, and <?xml-stylesheet …?> has type, href |
For full options, error classes, and usage examples, see the root README.
See Also
License
MIT