How to use the html to xml converter
- Paste your HTML source code into the input area.
- The converter tokenizes the markup immediately as you type or paste.
- Review the XML output where void elements are self-closed and text is escaped.
- Copy the result for use in an XML parser, API payload or configuration file.
How the conversion works
A standard HTML document is not valid XML because it allows unclosed void elements like <br> and <img>, unescaped ampersands in text, and declarations such as <!DOCTYPE html> that have no XML equivalent. This html to xml converter addresses each issue using a hand-written tokenizer that scans the input character by character.
The tokenizer identifies five node types: opening tags, closing tags, comments, doctypes and raw text. When it encounters a void element, it rewrites the tag as self-closing. For example, <hr> becomes <hr />. Text nodes are scanned for bare ampersands and angle brackets, which are replaced with their entity equivalents. Doctypes are removed entirely because XML uses a different declaration syntax.
Void elements reference
HTML defines a fixed set of void elements that must never have a closing tag. In XML every element must be explicitly closed, either with a matching end tag or with the self-closing slash. The converter handles all standard HTML5 void elements automatically.
| HTML tag | XML output |
|---|---|
<br> | <br /> |
<hr> | <hr /> |
<img src="x"> | <img src="x" /> |
<input type="text"> | <input type="text" /> |
<meta charset="utf-8"> | <meta charset="utf-8" /> |
<link rel="stylesheet"> | <link rel="stylesheet" /> |
Handling script and style blocks
In HTML the contents of <script> and <style> tags are treated as raw text. Characters like < and & are interpreted literally by the browser rather than as markup. XML does not have this concept, so placing JavaScript operators directly inside an element would cause a parse error.
To preserve the original code without altering it, the converter wraps the inner content of these tags in a <![CDATA[ ... ]]> section. CDATA tells the XML parser to treat everything inside as character data. If the script already contains the sequence ]]>, the converter splits it into two adjacent CDATA sections so the output remains valid.
Entity and attribute processing
HTML is lenient about unescaped ampersands. A string like Tom & Jerry renders correctly in a browser even if written as Tom & Jerry. XML requires every ampersand that is not part of an entity reference to be escaped. The converter detects bare ampersands in text nodes and replaces them with &, while leaving the five XML entities and numeric references untouched. HTML-only names such as © and do not exist in XML, so they are rewritten as numeric references (©,  ), and an unknown name such as &foo; becomes &foo;. Quoted attribute values keep their entities too, and duplicate attributes are dropped, keeping the first.
Attribute values are also normalized. HTML permits unquoted attributes such as <div class=main>, but XML mandates quotes around every attribute value. The tokenizer captures unquoted values and wraps them in double quotes during output. If you need to further process the resulting markup, tools like xml formatter can indent it, or xml viewer can display the tree structure.
Common mistakes when converting manually
Developers sometimes try to convert HTML to XML using simple find-and-replace operations, which leads to subtle errors. Replacing every > with /> will break closing tags and text content. Similarly, globally replacing & with & will double-escape entities that are already correct, turning < into &lt;.
Elements whose end tag is optional in HTML (li, p, td, tr and similar) are closed for you, but other unclosed or mismatched tags give an error, an attribute name XML cannot hold (such as @click) gives an error, and several top-level nodes produce a note because an XML document needs one root. Namespace prefixes such as xlink:href are kept as written, so declare the namespace in your XML if a namespace-aware parser will read the result. Another frequent mistake is forgetting to handle the doctype. An HTML5 doctype <!DOCTYPE html> is not valid XML syntax. Leaving it in the output will cause any strict XML parser to reject the document. The converter strips doctypes automatically. For documents that need an XML declaration, add <?xml version="1.0" encoding="UTF-8"?> manually at the top of the output after conversion.
Worked examples
| Example | Input | Result |
|---|---|---|
| Void elements and entities | HTML input: <div><img src="a.png" alt="A"><br><p>Tom & Jerry</p></div> | <div><img src="a.png" alt="A" /><br /><p>Tom & Jerry</p></div> |
| Doctype and comments | HTML input: <!DOCTYPE html> / <html> / <!-- nav --> / <body><hr></body> / </html> | / <html> / <!-- nav --> / <body><hr /></body> / </html> |
| Script and style blocks | HTML input: <script>var x = 1 < 2 && 3 > 1;</script> / <style>.a > .b { color: red; }</style> | <script><![CDATA[var x = 1 < 2 && 3 > 1;]]></script> / <style><![CDATA[.a > .b { color: red; }]]></style> |
FAQ
Why is HTML not valid XML?
HTML allows unclosed void elements like br and img, unquoted attribute values and unescaped ampersands. XML requires every element to be closed, all attributes to be quoted and special characters to be escaped.
What happens to the DOCTYPE declaration?
The HTML doctype is removed because XML uses a different declaration format. You can prepend an XML declaration manually if your target system requires one.
Are script and style contents escaped?
No. The converter wraps the contents of script and style tags in CDATA sections so that characters like less-than signs and ampersands remain intact without needing entity escaping.
Does this tool upload my code to a server?
No. The tokenizer runs entirely in your browser using JavaScript. Your HTML is never sent over the network.
Can I convert the XML back to HTML later?
Yes. Because the conversion preserves the original structure and only adds XML-required syntax like self-closing slashes, you can use a tool like xml to html to reverse the process.
How are unquoted HTML attributes handled?
The tokenizer reads unquoted attribute values and outputs them wrapped in double quotes, which is required by XML.