Overview
Introduction
HTML and XML are both tag trees, but HTML tolerates shortcuts XML doesn't allow: unclosed void elements, boolean attributes, unquoted attribute values. This tool bridges that gap, taking reasonably well-formed HTML and producing strict, well-formed XML from it.
It's a natural addition alongside this category's other markup and config round trips (INI, TOML, Properties, CSV, TSV all convert both ways here), and this site's yaml category already has an HTML to YAML converter built the same way, just targeting XML instead.
What Is HTML to XML Converter?
An HTML-to-XML converter scans HTML tag by tag with a lightweight hand-rolled tokenizer (no DOM globals, so it runs the same during server-side rendering as in the browser) and builds an XML element tree from what it finds.
Known HTML void elements are marked self-closing, attribute values are always quoted (and unquoted or missing values are normalized), and text content has XML's reserved characters (&, <, >) escaped, then the whole tree is serialized with this category's shared XML pretty-printer.
How HTML to XML Converter Works
The tokenizer walks the input recognizing <!-- comments --> (skipped), <!DOCTYPE ...> and similar declarations (skipped), closing tags </name> (matched against the currently open tag), opening tags <name attr="value" ...> (attributes parsed with quoted, unquoted, or boolean/valueless forms), and everything else as text.
Void elements (br, img, input, and the rest of a fixed 13-element list) are always emitted self-closed. script, style, textarea, and title bodies are captured as raw text up to their literal closing tag rather than parsed as markup. The full parsed tree is wrapped in a single <root> element and handed to this category's shared toPrettyXml serializer, which indents it and adds the XML declaration.
When To Use HTML to XML Converter
Use it when you need HTML markup represented as strict, well-formed XML, for an XSLT pipeline, an XML-only parser, or a system that flatly rejects HTML's more permissive syntax.
It's also useful for normalizing a scraped or hand-written HTML fragment's attribute quoting and void-element syntax without doing it by hand.
Often used alongside HTML to YAML, XML Validator and XML Prettifier.
Features
Advantages
- Normalizes all three of HTML's XML-incompatible shortcuts: unclosed void elements, boolean attributes, and unquoted attribute values.
- Works without any DOM globals, so it runs identically during server-side rendering and in the browser.
- Treats script/style/textarea/title content as raw text, avoiding the classic bug where embedded < characters in inline JavaScript break a naive tag scanner.
- Reports a specific, line-numbered error for mismatched or unclosed tags rather than silently producing malformed output.
Limitations
- This is not a full browser-grade HTML5 parser: it assumes reasonably well-formed input (properly nested and closed tags) and doesn't implement the HTML5 spec's error-recovery algorithm for malformed markup.
- Comments and <!DOCTYPE>/other declarations are dropped entirely rather than represented in the XML output.
- Only a handful of named HTML entities (amp, lt, gt, quot, apos, nbsp) are decoded by name during parsing; any other named entity is left as literal text (numeric and hex entities always decode).
Examples
Best Practices & Notes
Best Practices
- Make sure input HTML is reasonably well-formed (tags properly closed and nested) before converting; this tool reports mismatches as errors rather than guessing at browser-style recovery.
- Expect a <root> wrapper around the output every time, even for a single top-level element, and strip it downstream if your consumer expects the original tag as the document root.
- Run the result through XML Validator afterward if you're feeding it into a strict XML pipeline.
Developer Notes
The tag scanner is adapted from the same design as this site's yaml-category HTML to YAML converter (html-to-yaml.ts): a single-pass, hand-rolled recursive-descent parser with a position cursor and lazy line/column computation, duplicated locally here rather than imported across the category boundary. The one structural difference is the output target: instead of building a generic tag/attributes/children/text object tree, this parser constructs xml-core.ts's XmlNode tree directly (attributes as ordered [name, value] pairs, an explicit selfClosing flag per void element) and serializes it with toPrettyXml, which is where attribute-value escaping happens; text-node escaping (&, <, >) is done in the parser itself before the node is built, since toPrettyXml doesn't re-escape text content.
HTML to XML Converter Use Cases
- Converting scraped or hand-written HTML into well-formed XML for an XSLT or strict-XML pipeline
- Normalizing inconsistent attribute quoting and unclosed void elements in a legacy HTML fragment
- Generating an XML test fixture from a small HTML snippet
- Feeding an HTML page fragment into a system that only accepts valid XML
Common Mistakes
- Pasting malformed or browser-tolerant-but-technically-invalid HTML (badly nested tags relying on HTML5's error recovery) and expecting it to parse the way a browser would render it; this tool reports a mismatch error instead.
- Expecting the original top-level HTML element to be the XML document's root; it's always wrapped in a synthetic <root> element instead.
- Forgetting that comments and <!DOCTYPE> declarations are silently dropped from the output.
Tips
- If parsing fails with a mismatched-tag error, check for an unclosed or improperly nested tag right around the reported line.
- Use HTML to YAML first if you want to inspect the tree shape before committing to the XML conversion.
- For a large HTML document, convert one meaningful fragment at a time rather than an entire page.