All files / xml / parse.ts

100.00% Branches 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
 
 
 
 
 
 
 
 
 
 
x3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x3
x15
x15

































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

/**
 * Builds an in-memory XML document tree from a string.
 *
 * @module
 */

import type { ParseOptions, XmlDocument } from "./types.ts";
import { parseSync } from "./_parse_sync.ts";

export type { ParseOptions } from "./types.ts";

/**
 * Parses an XML string into a document tree.
 *
 * @example Usage
 * ```ts
 * import { parse } from "@std/xml/parse";
 * import { assertEquals } from "@std/assert";
 *
 * const doc = parse(`<root id="1"><child/></root>`);
 * assertEquals(doc.root.name.local, "root");
 * assertEquals(doc.root.attributes["id"], "1");
 * ```
 *
 * @param xml The XML string to parse.
 * @param options Options to control parsing behavior.
 * @returns The parsed document.
 * @throws {XmlSyntaxError} If the XML is malformed or has no root element.
 */
export function parse(xml: string, options?: ParseOptions): XmlDocument {
  return parseSync(xml, options);
}