How to pretty print XML
- Paste XML or open a file such as a sitemap, RSS feed, SVG, Maven
pom.xml, Android layout or SOAP response. - The formatted XML appears on the right with one element per line and nested elements indented.
- Set the indent to 2 spaces, 4 spaces or tabs, put each attribute on its own line, or remove comments.
- 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.
- 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
| Error | Cause | Fix |
|---|---|---|
| Opening and ending tag mismatch | Tags closed in the wrong order or a typo in a name | Close the innermost element first; names are case-sensitive |
| EntityRef: expecting ';' | A bare & in text, often in URLs | Write & |
| StartTag: invalid element name | A bare < in text | Write < or use CDATA |
| Extra content at the end of the document | More than one root element | Wrap everything in a single root element |
| Attribute redefined | The same attribute twice on one element | Keep one of them |
| AttValue: " or ' expected | An unquoted attribute value | Quote it: id="7" |
The five characters with predefined entities are < (<), > (>), & (&), ' (') and " ("), 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.