/** * DOM XML Parser * * Builds an in-memory XML tree ({@link XmlDocument} / {@link XmlElement}) * from an XML string. Built on top of {@link SaxParser} — no duplicate * parsing logic. * * For large documents, prefer SAX-based streaming. * This is intended for small-to-medium XML where tree access is convenient. */ import type { ToPlainObjectOptions, XmlDocument, XmlElement, XmlNode, XmlParseOptions } from "./types.js"; /** * Parse an XML string into a DOM tree. * * @param xml - Complete XML string. * @param options - Parse options. * @returns The parsed XML document. * @throws {XmlParseError} If the XML is malformed. * * **Fragment mode** (`{ fragment: true }`): suppresses the "multiple root * elements" error. The returned `XmlDocument.root` is the first root element, * and `XmlDocument.roots` contains all root-level elements. * * @example * ```ts * const doc = parseXml('text'); * console.log(doc.root.name); // "root" * console.log(doc.root.children[0].name); // "child" * ``` */ declare function parseXml(xml: string, options?: XmlParseOptions): XmlDocument; /** * Find the first child element with the given name. */ declare function findChild(element: XmlElement, name: string): XmlElement | undefined; /** * Find all child elements with the given name. */ declare function findChildren(element: XmlElement, name: string): XmlElement[]; /** * Get the concatenated text content of an element (recursive). */ declare function textContent(node: XmlNode): string; /** * Get an attribute value, or undefined if not present. */ declare function attr(element: XmlElement, name: string): string | undefined; /** * Walk all descendant elements depth-first, calling visitor for each. */ declare function walk(element: XmlElement, visitor: (el: XmlElement) => void): void; /** * Convert an {@link XmlElement} DOM tree to a plain JavaScript object. * * Produces a plain JavaScript object where element names become object keys, * attributes are prefixed (default `@_`), text-only elements collapse to their * string value, and repeated sibling names merge into arrays. * * @param element - The element to convert. * @param options - Conversion options. * @returns A plain JavaScript object representing the element. * * @example * ```ts * const doc = parseXml('text'); * const obj = toPlainObject(doc.root); * // { root: { "@_attr": "1", child: "text" } } * ``` * * @example * ```ts * const doc = parseXml('ab'); * const obj = toPlainObject(doc.root); * // { list: { item: ["a", "b"] } } * ``` */ declare function toPlainObject(element: XmlElement, options?: ToPlainObjectOptions): Record; export { parseXml, findChild, findChildren, textContent, attr, walk, toPlainObject };