Home › Developer Tools › XML Formatter

XML Formatter

Paste XML or open a file to beautify, minify or validate it. Errors are reported with the line and column, comments and CDATA are kept, and text content is never changed.

Input
Waiting for XML…

      
      

Everything runs in your browser. Files you open are read locally and never uploaded.

Advertisement

How to use the XML Formatter

  1. Paste your XML into the input box, open an .xml file, or press Sample.
  2. Press Format XML and choose 2 spaces, 4 spaces or tabs. The output updates as you type.
  3. Check the status bar. A green tick means the XML is well-formed. Otherwise you get the error message with its line and column; press Go to error to jump there.
  4. Press Minify to remove indentation and line breaks between tags, optionally removing comments.
  5. Switch to Tree to fold elements and click any node to see its path, then copy or download the result.

How the formatter keeps your data intact

Many online beautifiers re-flow every text node, which silently changes data. This XML formatter follows four rules so the output means exactly the same as the input:

  • Only whitespace between tags is changed. Gaps made purely of spaces and line breaks between elements are replaced by consistent indentation.
  • Text stays inline. An element that contains only text, such as <price>44.95</price>, is printed on one line with its text untouched.
  • Mixed content is left alone. In <p>An <b>in-depth</b> look</p> the spaces around <b> are part of the text, so the whole element is kept on one line exactly as written.
  • xml:space="preserve" is respected, and comments, CDATA sections, processing instructions and the DOCTYPE are copied verbatim.

Before and after

Input:

<order id="7"><item sku="A1"><name>Lamp</name><qty>2</qty></item><!-- gift --></order>

Formatted with 2 spaces:

<order id="7">
  <item sku="A1">
    <name>Lamp</name>
    <qty>2</qty>
  </item>
  <!-- gift -->
</order>

Common XML errors and how to fix them

Error messageTypical causeFix
Opening and ending tag mismatchTags closed in the wrong order, or a typo in a closing tagClose the innermost element first: <a><b></b></a>
Extra content at the end of the documentMore than one root elementWrap everything in a single root element
Entity 'nbsp' not definedHTML entities used in XMLUse &#160; or declare the entity in a DTD
xmlParseEntityRef: no nameA bare & in text or an attributeWrite &amp; or wrap the text in CDATA
AttValue: " or ' expectedAttribute value without quotesQuote every value: id="5"
XML declaration allowed only at the startSpaces, a blank line or a second <?xml ?> before the declarationMake <?xml ... ?> the very first characters of the file

Frequently asked questions

What does an XML formatter do?

It re-indents XML so that each nested element starts on its own line with consistent indentation, making the structure easy to read. It only changes whitespace between tags; element names, attributes, text, comments and CDATA sections stay the same.

Does formatting change my data?

No. Whitespace-only gaps between elements are re-indented, but text content is kept exactly as written. Elements with short text, such as <name>Bob</name>, stay on one line, and mixed content (text with inline tags) and elements with xml:space="preserve" are left untouched.

How do I validate XML?

Paste the XML and look at the status bar. The validator uses your browser's XML parser to check that the document is well-formed: one root element, every tag closed and correctly nested, quoted attributes and valid entities. If not, it shows the first error with the line and column.

Does this check my XML against a schema (XSD or DTD)?

No. It checks well-formedness, which is what most parsers require first. Validating against an XSD or DTD needs a schema-aware validator such as xmllint or an IDE.

Why do I get an error about an undefined entity like &nbsp;?

XML only predefines five entities: &amp;, &lt;, &gt;, &quot; and &apos;. HTML entities such as &nbsp; or &copy; are not valid in plain XML unless a DTD declares them. Use a numeric reference instead, for example &#160; for a non-breaking space.

What is the difference between formatting and minifying XML?

Formatting adds line breaks and indentation for people to read. Minifying removes that whitespace between tags to make the file smaller for storage or transfer. Both keep the same data.

Is my XML uploaded to a server?

No. Parsing, formatting and validation all run in your browser, and files you open are read locally.

Related tools