XML Formatter & Validator

Pretty print, minify and check XML for errors. Runs in your browser.

Indent
Attributes
Comments
Input1 lines
Output
<?xml version="1.0" encoding="UTF-8"?>
<!-- Product feed -->
<catalog xmlns="urn:example:catalog" updated="2026-10-22">
  <book id="bk101" lang="en">
    <title>XML Developer's Guide</title>
    <author>Gambardella, Matthew</author>
    <price currency="USD">44.95</price>
    <notes><![CDATA[Ships in <b>2 days</b> & free returns]]></notes>
  </book>
  <book id="bk102" lang="en">
    <title>Midnight Rain</title>
    <author>Ralls, Kim</author>
    <price currency="USD">5.95</price>
    <available/>
  </book>
</catalog>
Well-formed XML · 16 lines

How to pretty print XML

  1. Paste XML or open a file such as a sitemap, RSS feed, SVG, Maven pom.xml, Android layout or SOAP response.
  2. The formatted XML appears on the right with one element per line and nested elements indented.
  3. Set the indent to 2 spaces, 4 spaces or tabs, put each attribute on its own line, or remove comments.
  4. If the document is not well-formed, the status line shows the line and column of the first error and the line is marked in the input.
  5. Switch to Tree to fold and unfold elements, then copy or download the result.

Well-formed vs valid XML

The XML 1.0 specification (Fifth Edition, W3C Recommendation of November 26, 2008) defines two levels. A document is well-formed when it meets the syntax constraints; any XML parser must reject a document that is not. It is valid when it is well-formed and also conforms to a DTD or a schema language such as XSD. This validator checks the first level, which is what breaks most files in practice.

Common XML errors and how to fix them

ErrorCauseFix
Opening and ending tag mismatchTags closed in the wrong order or a typo in a nameClose the innermost element first; names are case-sensitive
EntityRef: expecting ';'A bare & in text, often in URLsWrite &amp;
StartTag: invalid element nameA bare < in textWrite &lt; or use CDATA
Extra content at the end of the documentMore than one root elementWrap everything in a single root element
Attribute redefinedThe same attribute twice on one elementKeep one of them
AttValue: " or ' expectedAn unquoted attribute valueQuote it: id="7"

The five characters with predefined entities are &lt; (<), &gt; (>), &amp; (&), &apos; (') and &quot; ("), as listed in section 4.6 of the specification.

Pretty print XML on the command line and in Python

On Linux and macOS, xmllint from libxml2 reformats a file and fails on errors:

xmllint --format input.xml > pretty.xml

In Python 3.9 or later, the standard library can indent an element tree in place:

import xml.etree.ElementTree as ET
tree = ET.parse("input.xml")
ET.indent(tree, space="  ")
tree.write("pretty.xml", encoding="utf-8", xml_declaration=True)

How to format XML in VS Code and Notepad++

VS Code does not format XML without an extension. After installing an XML language extension such as Red Hat's XML, run Format Document (Shift+Alt+F on Windows, Shift+Option+F on macOS, Ctrl+Shift+I on Linux). In Notepad++, install the XML Tools plugin from Plugins > Plugins Admin, then use Plugins > XML Tools > Pretty print.

Minify XML

Minify removes the whitespace-only text between elements and normalizes spacing inside tags, which makes payloads for APIs and config files smaller. Text content, CDATA and comments stay unless you choose to remove comments.

Sources: W3C, Extensible Markup Language (XML) 1.0 (Fifth Edition), sections 2.1, 2.8, 2.10 and 4.6; Python documentation, xml.etree.ElementTree.indent (added in 3.9); libxml2 xmllint manual. Checked October 2026.

Frequently asked questions

Is my XML uploaded anywhere?

No. Formatting, minifying and validation all run in your browser. Files you open are read locally and are not sent to a server.

What is the difference between well-formed and valid XML?

Well-formed XML follows the syntax rules of the XML 1.0 specification: one root element, every tag closed and properly nested, quoted attribute values and escaped special characters. Valid XML is also well-formed and additionally matches a DTD or XML Schema (XSD). This tool checks well-formedness.

Can it validate against an XSD schema?

No. Schema validation needs the XSD file and a validating parser. Use xmllint --schema schema.xsd file.xml on the command line, or your IDE, for that. This page tells you whether the document itself is well-formed and where the first error is.

Does pretty printing change my data?

Formatting adds line breaks and indentation between elements and trims leading and trailing whitespace in text. If whitespace inside an element is significant, for example where xml:space="preserve" is used, check the result or keep the original. Comments, CDATA sections, processing instructions and attribute values are kept.

How do I fix "Opening and ending tag mismatch"?

A closing tag does not match the most recent open tag. Go to the line in the error message, and look upward for the element that was opened but not closed, or for a typo in the tag name. XML tag names are case-sensitive, so <Item> and </item> do not match.

How do I write & or < inside XML text?

Escape them as &amp; and &lt;. The five predefined entities are &lt; &gt; &amp; &apos; and &quot;. For longer blocks of markup-like text, wrap the content in a CDATA section.