XML Formatter & Beautifier
Format, beautify, validate, or minify XML online. Indent nested elements, normalise self-closing tags, and turn minified XML into a readable structure. Invalid XML is reported with a detailed parser error.
Tip: Click in the XML and copy (Ctrl/Cmd+C) to copy the path at the caret (element►root►item).
What is XML?
XML (Extensible Markup Language) is a text format for hierarchical data, defined by the W3C in the XML 1.0 Recommendation. Unlike HTML, it has no fixed vocabulary: you decide what <invoice> or <customer> mean. XML is still common in configuration files, SOAP web services, RSS and Atom feeds, sitemaps, and document formats such as SVG and DOCX (a zipped bundle of XML files). Its rules are strict: one root element, every tag closed and correctly nested, quoted attribute values and escaped special characters. A document that follows these rules is called well-formed.
How to use the XML formatter
- Paste XML into the Input pane, or load it with File or URL (your browser fetches the address directly, so the remote server must allow cross-origin requests).
- Choose an Indent and a Self-closing mode.
- Click Beautify to indent the document or Minify to put it on one line. Both first check that the input is well-formed.
- Use Tree Viewer to explore the structure, then Copy or Download the result as
formatted.xml.
Options explained
- Indent sets 2 spaces, 4 spaces or a tab for each nesting level. It applies to Beautify.
- Self-closing: Keep as-is leaves empty elements exactly as written.
- Collapse empty tags to <tag/> rewrites an element that contains nothing, or only whitespace, such as
<note></note>, as<note/>. - Expand <tag/> to <tag></tag> does the reverse. Some older consumers and templating systems expect explicit end tags. Both forms mean the same thing to an XML parser.
- Tree Viewer shows elements as a collapsible tree that follows the Input as you type. Attributes appear as
@nameand repeated siblings are numbered, e.g.item[0]. Clicking a node copies its path.
The Self-closing setting applies to Minify as well as Beautify.
How Beautify and Minify treat your document
The tool first parses the input with your browser's built-in XML parser (DOMParser) to make sure it is well-formed. It does not check a DTD or XML Schema, so a well-formed document that breaks your schema still passes. If parsing succeeds, the original text is reformatted. The only whitespace either mode removes is whitespace-only text between tags; text, attribute values, CDATA sections, comments and processing instructions are copied exactly as written.
- Beautify puts each element that contains only other elements (and perhaps comments or processing instructions) on its own lines, indenting its children one level deeper. An element that contains any text or CDATA is written on a single line exactly as in the input: that covers text-only elements such as
<item>Hi</item>and mixed content such as<p>Hello <b>world</b> again</p>, so no text is split or re-spaced. The XML declaration, the DOCTYPE, and comments and processing instructions between elements each get their own line. Entity references such as&and are left exactly as written. - Minify removes whitespace-only text between tags and leaves everything else alone. Inside an element that also holds text, whitespace between child elements counts as part of the text and is kept, so
<p><b>a</b> <i>b</i> text</p>keeps the space between</b>and<i>. Line breaks inside a text value, CDATA section or comment stay, so the result is not always a single line.
The tool does not read xml:space, so whitespace-only text between child elements is removed even inside an element marked xml:space="preserve". The Self-closing options also rewrite empty elements anywhere in the document, including inside mixed content.
Common errors and how to fix them
Problems are reported above the panes after "Invalid XML:", and the Output pane shows "Not valid". The tool takes the first error from your browser's parser, drops the surrounding boilerplate and, where the parser gives a position, starts the message with the line and column. In current Chromium, for example, <a><b></a> gives "Invalid XML: Line 1, column 10: Unexpected closing tag: a != b". The wording after the position comes from the browser, so Firefox and Safari describe the same problem differently.
| Input | Cause | Fix |
|---|---|---|
<a><b></a> | Unclosed or wrongly nested element | Close inner elements before outer ones: <a><b></b></a> |
<Root></root> | Tag names are case-sensitive | Use the same case in start and end tags |
<a>Fish & Chips</a> | Bare & or < in text | Write & and <, or use a CDATA section |
<r>a b</r> | HTML entity names are not defined in XML | Use a numeric reference such as   |
<a/><b/> | More than one root element | Wrap everything in a single root element |
<a x=1/> | Unquoted attribute value | Quote it: x="1" |
<a x="1" x="2"/> | Same attribute twice on one element | Keep one of them |
<x:a/> | Namespace prefix not declared | Add xmlns:x="..." to the element or an ancestor |
A comment before <?xml ...?> | The XML declaration must be the very first thing in the document | Move or delete whatever comes before it |
Examples
| Input | Action | Output |
|---|---|---|
<root><item id="1">Hi</item></root> | Beautify, 2 spaces | <root> |
<book><tags><tag>xml</tag></tags><note/></book> | Beautify, Expand <tag/> | <book> |
<r><e></e></r> | Minify, Collapse empty tags | <r><e/></r> |
<doc><p>Hello <b>world</b> again</p><list><i>1</i><i>2</i></list></doc> | Beautify, 2 spaces | <doc> |
<r> | Minify | <r><x a="1 2"> two words </x></r> |
Frequently asked questions
Is my XML uploaded to a server?
No. Parsing and formatting run in JavaScript in your browser, and the page does not send your input anywhere. The only request involving your data is the one you start with the URL button, which your browser makes directly to the address you enter.
Does this validate against a schema or DTD?
No. It checks well-formedness only: correct nesting, a single root, quoted attributes, escaped characters and declared namespace prefixes. Validating against an XSD or DTD needs a schema-aware validator.
Why do the error messages look different in another browser?
The check uses each browser's own XML parser, and Chrome, Firefox and Safari word their messages differently. The line and column, and the underlying problem, are what to look for.
Can formatting change the text inside my elements?
No. Beautify keeps any element that contains text, including mixed content, on one line exactly as written, and Minify removes only whitespace-only text between tags. Attribute values, CDATA sections, comments and processing instructions are never altered. The one exception is the Self-closing option, which rewrites empty elements wherever they appear.
Is <tag/> the same as <tag></tag>?
Yes. The XML specification treats an empty-element tag and a start tag immediately followed by its end tag as the same empty element. The Self-closing option only changes how it is written.
Is XML the same as HTML?
No. XML is a strict, general-purpose format with no predefined tags. HTML has a fixed set of elements, and browsers recover from many mistakes in it. For web pages, use the HTML Formatter.