XML Formatter
CodePretty-print and validate XML with proper indentation. Uses your browser's built-in parser — your XML is never uploaded or stored anywhere.
What is a XML?
The XML Formatter takes a compact, single-line, or inconsistently indented XML document and pretty-prints it with consistent indentation and each element on its own line, making the document's hierarchical structure immediately readable. It uses your browser's native XML parser to validate the input before formatting, so any parse error is caught and reported rather than silently producing malformed output.
Compact XML is common from API responses, database exports, log files, and tools that generate XML programmatically. A SOAP response or Android resource file that arrives as a single dense string is technically valid but humanly unreadable:
<?xml version="1.0"?><catalog><book id="1"><title>Clean Code</title><author>Robert Martin</author></book></catalog>
The XML Formatter expands this into an indented tree where every parent-child relationship is visible through indentation and every element's content is easy to locate:
<?xml version="1.0"?>
<catalog>
<book id="1">
<title>Clean Code</title>
<author>Robert Martin</author>
</book>
</catalog>
Unlike a simple regex-based approach, this formatter parses the XML into a DOM tree using the browser's built-in DOMParser — the same engine that renders XML in your browser. This means it correctly handles namespaces, entity references, processing instructions, and CDATA sections. If the input has a structural error (unclosed tag, invalid character, mismatched nesting), the formatter reports the parse error rather than formatting broken XML.
All processing runs locally in your browser. No XML data is sent to any server. This makes it safe for SOAP envelopes containing credentials, configuration files with secrets, or XML exports of private business data. Use the JSON Formatter for JSON API responses and CSV to JSON Formatter for tabular data.
How to use this XML calculator
- Paste your XML into the Raw XML input box — this can be a single-line API response, a minified config file, or any well-formed XML document.
- Choose an Indent Size from the dropdown: 2 spaces (default) or 4 spaces.
- The Formatted XML output updates instantly. If the XML is invalid, the output will show a parse error message.
- Review the output to confirm the structure matches your expectations.
- Click Copy to copy the formatted XML to your clipboard.
Formula & Methodology
The formatter uses a two-phase approach: Phase 1 — Parse: The input is passed toDOMParser.parseFromString(input, 'application/xml'). The browser's XML parser builds a full DOM tree, validating well-formedness in the process. If a<parsererror>element appears in the result, the parse failed and the error message is returned. Phase 2 — Serialise: The DOM tree is traversed recursively. Each node type is handled as follows: - Element nodes: Print<tagName attributes>at the current indent depth. If the element has a single text-only child, print it inline:<tag>value</tag>. Otherwise, recursively serialise each child at depth + 1 and close with</tagName>at the original depth. Empty elements are serialised as<tagName attributes />. - Text nodes: Trimmed and printed at the current indent depth; empty text nodes (whitespace-only) are skipped. - Comment nodes: Printed as<!-- content -->at the current indent depth. - Processing instructions: Printed as<?target data?>at the current indent depth. Before:xml <?xml version="1.0"?><catalog><book id="1"><title>The Pragmatic Programmer</title><author>David Thomas</author></book></catalog>After (2 spaces):xml <?xml version="1.0"?> <catalog> <book id="1"> <title>The Pragmatic Programmer</title> <author>David Thomas</author> </book> </catalog>