interface Document { declaration: { attributes: Record<string, string>; }; root: { name: string; attributes: Record<string, string>; children: Xml[]; } | undefined;}
interface Xml { name: string; attributes: Record<string, string>; content?: string; children: Xml[];}
export function parse(xml: string): Document { xml = xml.trim();
xml = xml.replace(/<!--[\s\S]*?-->/g, "");
return document();
function document(): Document { return { declaration: declaration(), root: tag(), }; }
function declaration() { const m = match(/^<\?xml\s*/); if (!m) return;
const node: any = { attributes: {}, };
while (!(eos() || is("?>"))) { const attr = attribute(); if (!attr) return node; node.attributes[attr.name] = attr.value; }
match(/\?>\s*/);
return node; }
function tag() { const m = match(/^<([\w-:.]+)\s*/); if (!m) return;
const node: Xml = { name: m[1], attributes: {}, children: [], };
while (!(eos() || is(">") || is("?>") || is("/>"))) { const attr = attribute(); if (!attr) return node; node.attributes[attr.name] = attr.value; }
if (match(/^\s*\/>\s*/)) { return node; }
match(/\??>\s*/);
node.content = content();
let child; while ((child = tag())) { node.children.push(child); }
match(/^<\/[\w-:.]+>\s*/);
return node; }
function content() { const m = match(/^([^<]*)/); if (m) return entities(m[1]); return ""; }
function attribute() { const m = match(/([\w:-]+)\s*=\s*("[^"]*"|'[^']*'|\w+)\s*/); if (!m) return; return { name: m[1], value: entities(strip(m[2])) }; }
function strip(val: string) { return val.replace(/^['"]|['"]$/g, ""); }
function entities(val: string) { return val.replaceAll("<", "<").replaceAll(">", ">").replaceAll("&", "&"); }
function match(re: RegExp) { const m = xml.match(re); if (!m) return; xml = xml.slice(m[0].length); return m; }
function eos() { return 0 == xml.length; }
function is(prefix: string) { return 0 == xml.indexOf(prefix); }}