@ende/xml
ende add jsr:@ende/xmlVersion 2026.10.15 on JSR.
XML parsing and serialization for Ende.
This module implements a non-validating parser for
XML 1.0 (Fifth Edition), with opt-in
support for XML 1.1 (Second Edition)
via the xmlVersion parsing option.
Parsing APIs
Three parsing APIs are provided for different use cases:
| API | Use Case | Output |
|---|---|---|
parse |
Parse a complete XML string | Document tree |
parseXmlStream |
Streaming with maximum throughput | Direct callbacks |
parseXmlRecords |
Streaming records assembled in callbacks | AsyncGenerator<T> |
Quick Examples
DOM-style parsing
import { parse } from "@ende/xml";
import { assertEquals } from "@ende/assert";
const doc = parse("<root><item>Hello</item></root>");
assertEquals(doc.root.name.local, "root");
High-performance streaming with callbacks
For maximum throughput when processing large files:
import { parseXmlStream } from "@ende/xml";
const response = await fetch("https://example.com/feed.xml");
const textStream = response.body!.pipeThrough(new TextDecoderStream());
let itemCount = 0;
await parseXmlStream(textStream, {
onStartElement(name) {
if (name === "item") itemCount++;
},
});
console.log(`Found ${itemCount} items`);
Streaming with byte streams
For convenience with fetch responses:
import { parseXmlStreamFromBytes } from "@ende/xml";
const response = await fetch("https://example.com/feed.xml");
await parseXmlStreamFromBytes(response.body!, {
onStartElement(name) {
console.log(`Element: ${name}`);
},
});
XML 1.1 mode
Pass xmlVersion: "1.1" to opt in to XML 1.1 parsing rules. The option is
independent of the document's <?xml version="..."?> declaration.
import { parse } from "@ende/xml";
import { assertEquals, assertThrows } from "@ende/assert";
// Accepted in XML 1.1, rejected in XML 1.0:
const xml = "<root></root>";
const doc = parse(xml, { xmlVersion: "1.1" });
assertEquals(doc.root.name.local, "root");
assertThrows(() => parse(xml));
DOCTYPE handling
<!DOCTYPE ...> declarations are rejected by default to avoid processing
hostile DTD content. Pass disallowDoctype: false to tolerate them in
trusted input (e.g. legacy XHTML or RSS feeds). DTD contents are still
ignored — only the five predefined entities (lt, gt, amp, apos,
quot) are ever expanded.
Position Tracking
Both parsers support optional position tracking (line, column, offset) for debugging and error reporting:
-
DOM parser (
parse): Position tracking is enabled by default to provide detailed error messages. Disable with{ trackPosition: false }for a performance boost when parsing trusted XML. -
Streaming parser (
parseXmlStream): Position tracking is disabled by default for optimal streaming performance. Enable with{ trackPosition: true }when you need position info.
Modules
@ende/xml/parse— Builds an in-memory XML document tree from a string.@ende/xml/parse-records— Async-generator adapter that turns an XML chunk source into a stream of application-defined records assembled inside SAX-style event callbacks.@ende/xml/parse-stream— Streaming XML parser with callback-based API for maximum throughput.@ende/xml/stringify— XML serialization module.@ende/xml/types— Type definitions for the XML parser and serializer.
Exports
BaseParseOptions— Base options shared by parsing functions.isCData— Type guard to check if a node is a CDATA node.isComment— Type guard to check if a node is a comment.isElement— Type guard to check if a node is an element.isText— Type guard to check if a node is a text node.parse— Parses an XML string into a document tree.ParseOptions— Options for parse.ParseStreamOptions— Options for parseXmlStream.parseXmlRecords— Parses an async iterable of XML chunks and yields records assembled inside SAX-style event callbacks.parseXmlRecordsFromBytes— Parses an async iterable of XML byte chunks and yields records assembled inside SAX-style event callbacks.parseXmlStream— Parse XML from a stream with maximum throughput using direct callbacks.parseXmlStreamFromBytes— Parse XML from a byte stream with maximum throughput using direct callbacks.stringify— Converts an XML document or element to an XML string.StringifyOptions— Options for stringify.XmlAttributeIterator— Reusable attribute accessor that avoids allocating attribute object arrays.XmlCDataNode— A CDATA section node in the XML tree.XmlCommentNode— A comment node in the XML tree.XmlContentCallback— Callback for text, CDATA, or comment content with position.XmlDeclaration— The XML declaration of a document, exposed as XmlDocument.declaration.XmlDeclarationCallback— Callback for XML declarations.XmlDoctypeCallback— Callback for DOCTYPE declarations.XmlDocument— A parsed XML document.XmlElement— An element node in the XML tree.XmlEventCallbacks— Callbacks for event-level output - enables zero-allocation event emission.XmlName— A qualified XML name with optional namespace prefix and resolved URI.XmlNode— Discriminated union of all node types in an XML tree.XmlPosition— Position information for error reporting.XmlProcessingInstructionCallback— Callback for processing instructions.XmlSyntaxError— Error thrown when XML parsing fails.XmlTextNode— A text node in the XML tree.