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.

Indent: Self-closing:
Copied
Input0 B
Output0 B

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

  1. 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).
  2. Choose an Indent and a Self-closing mode.
  3. Click Beautify to indent the document or Minify to put it on one line. Both first check that the input is well-formed.
  4. Use Tree Viewer to explore the structure, then Copy or Download the result as formatted.xml.

Options explained

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.

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.

InputCauseFix
<a><b></a>Unclosed or wrongly nested elementClose inner elements before outer ones: <a><b></b></a>
<Root></root>Tag names are case-sensitiveUse the same case in start and end tags
<a>Fish & Chips</a>Bare & or < in textWrite &amp; and &lt;, or use a CDATA section
<r>a&nbsp;b</r>HTML entity names are not defined in XMLUse a numeric reference such as &#160;
<a/><b/>More than one root elementWrap everything in a single root element
<a x=1/>Unquoted attribute valueQuote it: x="1"
<a x="1" x="2"/>Same attribute twice on one elementKeep one of them
<x:a/>Namespace prefix not declaredAdd xmlns:x="..." to the element or an ancestor
A comment before <?xml ...?>The XML declaration must be the very first thing in the documentMove or delete whatever comes before it

Examples

InputActionOutput
<root><item id="1">Hi</item></root>Beautify, 2 spaces<root>
  <item id="1">Hi</item>
</root>
<book><tags><tag>xml</tag></tags><note/></book>Beautify, Expand <tag/><book>
  <tags>
    <tag>xml</tag>
  </tags>
  <note></note>
</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>
  <p>Hello <b>world</b> again</p>
  <list>
    <i>1</i>
    <i>2</i>
  </list>
</doc>
<r>
  <x a="1  2">  two   words </x>
</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.

Guides

Related tools