How to format XML
Compact XML is hard to read and debug. This guide explains what XML formatting is, why it matters, and how to pretty-print XML safely — all using a free tool that runs entirely in your browser.
Try the XML FormatterWhat is XML?
XML (eXtensible Markup Language) is a text-based format for structuring data. It uses tags — like HTML — to describe elements, attributes and nested hierarchies. Unlike HTML, XML does not define any tags itself; you (or the application you are working with) define the tags that describe your data.
A simple XML document looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="1">
<title>Example Book</title>
<author>John Smith</author>
</book>
</catalog>
XML is used everywhere: configuration files, RSS feeds, SOAP APIs, SVG graphics, Office documents (.docx, .xlsx are XML inside a zip), Android layouts, and many enterprise systems.
What is an XML Formatter?
An XML Formatter takes XML text and adds consistent indentation and line breaks to make it easier to read. It parses the XML to confirm it is well-formed, then re-serialises it with your chosen indentation (2 spaces, 4 spaces or tabs).
Formatting does not change the structure or content of the XML — it only changes the presentation whitespace between elements. Attributes, namespaces, comments, CDATA blocks and text content are all preserved.
Why Format XML?
Formatting XML makes it significantly easier to work with:
- Easier debugging — indentation reveals the nesting structure so mismatched or unclosed tags are obvious.
- Better readability — a wall of compact XML is hard to scan; indented XML lets you see the hierarchy at a glance.
- Easier code review — when diffing XML files, consistent formatting makes changes stand out instead of being hidden by inconsistent whitespace.
- Understanding nested structures — deeply nested elements are much easier to follow when indented.
- Identifying malformed XML — a formatter validates before formatting, so a parse error tells you exactly where the XML is broken.
What is XML Pretty Printing?
XML pretty printing is another name for XML formatting. It adds whitespace (spaces or tabs) and line breaks to compact or unformatted XML so the document's structure is visible and the content is easier to read.
For example, this compact XML:
<catalog><book id="1"><title>Example</title><author>John</author></book></catalog>
becomes:
<catalog>
<book id="1">
<title>Example</title>
<author>John</author>
</book>
</catalog>
The data is identical — only the presentation has changed.
Does Formatting XML Change the Data?
No. A proper XML formatter preserves the XML structure, attributes, namespaces, comments, CDATA blocks and text content. Only the presentation whitespace (indentation and line breaks between elements) is changed.
Importantly, meaningful whitespace inside text nodes is preserved. If an element contains Hello World (with multiple spaces between the words), a correct formatter keeps those spaces — they are part of the data, not formatting. The formatter only removes and replaces whitespace between elements (inter-element whitespace that is purely for indentation).
XML Formatter vs XML Minifier
These two tools do opposite things:
- Formatter — adds formatting. It inserts indentation and line breaks to make XML easier for humans to read.
- Minifier — removes formatting. It strips unnecessary whitespace between elements to produce the most compact XML possible.
You might format XML when you need to read or edit it, and minify it for production use where size matters. Both operations preserve the XML content — they only change the presentation whitespace.
XML Formatter vs XML Validator
- Validator — checks whether your XML is well-formed (correct syntax, matching tags, proper nesting, valid attributes). It reports errors but does not change the XML.
- Formatter — improves readability by adding indentation. A formatter should validate first — if the XML is not well-formed, it cannot be formatted.
The XML Formatter validates before formatting. If your XML has a syntax error, the tool shows the error with the line and column where the problem was found, so you can fix it before formatting.
How to Format XML
- Paste your XML into the editor above, or upload a
.xmlfile. - Choose your indentation (2 spaces, 4 spaces or tabs).
- Click Format XML to validate and pretty-print the document.
- Copy or download the formatted result as
formatted.xml.
If your XML is invalid, the tool shows a clear error message with the line and column where the problem was found. Fix the error and try again — invalid XML cannot be formatted.
Format XML in your browser
Because XML formatting is a pure text operation, it runs entirely client-side. Your XML never leaves your device — important for privacy, speed and offline use. The browser's built-in DOMParser parses the XML without fetching external entities, so XXE (XML External Entity) attacks are not possible. Try the XML Formatter now, or use the JSON Validator to validate and format JSON.
Try these tools
XML Formatter
Format and beautify XML online with proper indentation and readable structure. Validate, format, minify, copy and download XML.
Use toolJSON Validator
Validate JSON online, find syntax errors, format and minify JSON with clear error messages and line and column information.
Use toolJSON Formatter
Format, validate and minify JSON instantly — all in your browser.
Use toolJSON Minifier
Minify JSON online by removing unnecessary whitespace while preserving your data. Validate, compress, copy and download compact JSON.
Use toolFrequently asked questions
Does formatting XML change the data?+
No. A proper formatter preserves the XML structure, attributes, namespaces, comments, CDATA and text content. Only the presentation whitespace between elements is changed.
What is the difference between formatting and validating XML?+
Validation checks whether your XML is well-formed. Formatting re-indents valid XML for readability. A formatter validates first — if the XML is not well-formed, it cannot be formatted.
Is formatting XML safe?+
Yes. When formatting runs in your browser, your data is never uploaded to a server. The browser's DOMParser parses the XML without fetching external entities, so XXE attacks are not possible.