All files / xml / mod.ts

100.00% Branches 0/0
100.00% Functions 0/0
100.00% Lines 4/4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x2
x2
x2
x2













































































































// Copyright 2018-2026 the Deno authors. MIT license.
// This module is browser compatible.

/**
 * XML parsing and serialization for Deno.
 *
 * This module implements a non-validating parser for
 * {@link https://www.w3.org/TR/xml/ | XML 1.0 (Fifth Edition)}, with opt-in
 * support for {@link https://www.w3.org/TR/xml11/ | XML 1.1 (Second Edition)}
 * via the `xmlVersion` parsing option.
 *
 * ## Parsing APIs
 *
 * Two parsing APIs are provided for different use cases:
 *
 * | API | Use Case | Output |
 * |-----|----------|--------|
 * | {@linkcode parse} | Parse a complete XML string | Document tree |
 * | {@linkcode parseXmlStream} | Streaming with maximum throughput | Direct callbacks |
 *
 * ## Quick Examples
 *
 * ### DOM-style parsing
 *
 * ```ts
 * import { parse } from "@std/xml";
 * import { assertEquals } from "@std/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:
 *
 * ```ts ignore
 * import { parseXmlStream } from "@std/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:
 *
 * ```ts ignore
 * import { parseXmlStreamFromBytes } from "@std/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.
 *
 * ```ts
 * import { parse } from "@std/xml";
 * import { assertEquals, assertThrows } from "@std/assert";
 *
 * // Accepted in XML 1.1, rejected in XML 1.0:
 * const xml = "<root>&#x1;</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 ({@linkcode 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 ({@linkcode parseXmlStream})**: Position tracking is
 *   **disabled by default** for optimal streaming performance. Enable with
 *   `{ trackPosition: true }` when you need position info.
 *
 * @module
 */

export * from "./types.ts";
export * from "./parse_stream.ts";
export * from "./parse.ts";
export * from "./stringify.ts";