XML Formatter
CodePretty-print and validate XML with proper indentation instantly. Uses your browser's built-in parser — your XML is never uploaded or stored anywhere.
Reviewed by the thecalcu.com team · Last updated July 17, 2026
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.
Why Use an XML Formatter?
Reading and editing compact XML by hand is significantly slower than working with formatted XML. Tracing a nested element in a single-line document requires counting opening and closing tags mentally, one miscount and you are editing the wrong element. Formatted XML lets you navigate by indentation level, just as you would read an outline.
Formatted XML is also essential for debugging API integrations. When a SOAP service or XML-based webhook returns an unexpected response, pasting the raw payload into the formatter immediately shows which elements are present, which are missing, and what the nesting structure looks like, without writing any code.
Version control diffs of XML are unreadable in minified form. A change to a single attribute value in a 2000-character single-line file appears as the entire line modified. Formatted XML produces line-level diffs that show exactly what changed.
Who Should Use This Formatter?
Back-end and integration developers working with SOAP APIs, XML-RPC endpoints, or enterprise middleware regularly receive XML payloads that need to be inspected or debugged. The formatter is the fastest path from a raw API response to a readable document.
Android developers editing layout XML files, resource files, or manifest files copied from another project can use the formatter to normalise indentation before incorporating the file into their project.
Data engineers processing XML exports from databases, ERP systems, or legacy enterprise applications need formatted XML to understand the schema before writing parsing logic. Pair the formatter with the CSV to JSON Formatter when converting XML data exports to a more workable format.
Students and learners working through XML tutorials or inspecting real-world XML files will find formatted output much easier to understand than the compact form, particularly for understanding how namespaces and attributes relate to element content.
What Insights Does the XML Formatter Give You?
Formatted XML immediately reveals:
- Nesting depth, deeply nested elements are obvious from their indentation level; a document that is seven levels deep is a candidate for flattening
- Attribute vs element content, the formatter keeps attributes inline with their element tag, making it easy to see which data is stored as attributes and which as child elements
- Element repetition, repeated sibling elements (array-like structures) are visible as a vertical list of same-name elements at the same indent level
- Empty elements, self-closing tags (
<item />) are distinguished from elements with content
The 2-space vs 4-space indent choice affects how much horizontal space deeply nested elements consume. Four spaces makes nesting depth more visually dramatic; two spaces keeps deeply nested documents on screen without horizontal scrolling.
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>
Frequently Asked Questions