HomeFormattersCodeXML Formatter

XML Formatter

Code

Pretty-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

  1. 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.
  2. Choose an Indent Size from the dropdown: 2 spaces (default) or 4 spaces.
  3. The Formatted XML output updates instantly. If the XML is invalid, the output will show a parse error message.
  4. Review the output to confirm the structure matches your expectations.
  5. 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 to DOMParser.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
What is an XML formatter?
An XML formatter (also called an XML pretty-printer or XML beautifier) takes a compact or single-line XML document and adds consistent indentation and newlines so each element appears on its own line. It makes the hierarchical structure of the document immediately visible without changing any data, attribute values, or element order. The XML Formatter on thecalcu.com uses your browser's built-in XML parser to validate the document before formatting.
What is the difference between XML and JSON?
Both XML and JSON are text-based formats for storing and transmitting structured data, but they differ in syntax and use. XML uses opening and closing tags (`<name>value</name>`) and supports attributes, namespaces, comments, and mixed content (text alongside child elements). JSON uses key-value pairs and arrays with no closing tag redundancy, making it more compact. XML is dominant in enterprise systems, SOAP web services, Android resources, and document formats (SVG, DOCX, XLSX); JSON is the default for REST APIs and JavaScript-heavy applications.
How does the XML formatter validate my input?
The formatter uses the browser's built-in `DOMParser` to parse the input as `application/xml`. If the input is not well-formed XML — for example, an unclosed tag, a missing namespace declaration, or invalid characters — the browser's parser returns a `<parsererror>` element instead of the document tree. The formatter detects this and returns the error message rather than formatting invalid input, so you always know whether your XML is valid before using the output.
What is well-formed XML?
Well-formed XML follows the W3C XML specification: it has exactly one root element, all tags are properly closed (including self-closing empty elements like `<br />`), all attribute values are quoted, tags are correctly nested (no overlapping tags), and special characters like `<`, `>`, `&`, `'`, and `"` are escaped as entities. Any XML that fails these rules is not parseable and the formatter will return a parse error.
How do I format XML?
Paste your XML into the Raw XML input box, choose an indent size (2 or 4 spaces), and the formatted output appears instantly. If the XML is invalid, an error message appears in the output box indicating what went wrong. Click the Copy button to copy the formatted XML.
Does the formatter preserve XML attributes?
Yes. All element attributes are preserved exactly as they appear in the source, including namespace declarations (`xmlns:`), attribute values, and ordering. The formatter only changes whitespace — it never modifies element names, attribute values, text content, or the document structure.
Does this work with SVG, RSS, or SOAP XML?
Yes. SVG, RSS/Atom feeds, SOAP envelopes, Maven POM files, Android layout XML, and any other well-formed XML document can be formatted. The formatter is namespace-aware and handles the `xmlns` attribute correctly. The only restriction is that the input must be well-formed XML — HTML that uses `<br>` without closing (non-XHTML) is not valid XML and will produce a parse error.
Is my XML uploaded anywhere?
No. The XML Formatter uses your browser's built-in `DOMParser` API to parse and your browser's DOM to serialise the output — all processing happens locally in your browser tab. No XML, whether it contains business data, configuration secrets, or personal information, is ever sent to a server or stored.
Does this work offline?
Yes — once the page has loaded, the formatter runs entirely without a network connection. Both the parsing and serialisation rely on browser-native APIs with no external dependencies.
What happens to the XML declaration?
If your XML starts with an XML declaration (`<?xml version="1.0" encoding="UTF-8"?>`), the formatter preserves it on the first line of the output, followed by the formatted document. If no declaration is present, none is added.
Can I use this for large XML files?
The formatter is limited by your browser's available memory, not by any server-side restriction. XML files up to a few hundred kilobytes format in under a second. Very large files (several megabytes) may cause the browser tab to pause briefly. For production-scale XML transformation, command-line tools like `xmllint --format` or `python -c 'import sys; import xml.dom.minidom; print(xml.dom.minidom.parse(sys.stdin).toprettyxml())'` are more appropriate.