What the formatter keeps
- Text exactly as written, including line breaks inside elements.
- Mixed content on one line, since whitespace there is part of the text.
- Comments, CDATA, processing instructions and DOCTYPE. Use “Remove comments” to drop comments.
-
Whitespace in
xml:space="preserve"elements, which tells every XML tool not to touch it.
For example, the paragraph below stays on one line while the surrounding structure is indented:
<article>
<title>Guide</title>
<p>Read the <a href="/docs">docs</a> first.</p>
</article>
Common XML errors
Mismatched closing tag
<order>
<item>Mug</order>
Line 2, column 12: Expected </item> (opened on line 2) but found </order>
Unescaped ampersand
<company>Johnson & Johnson</company>
Line 1, column 18: Unescaped "&": write & (or use a valid entity like <)
HTML entity
<price>10 EUR</price>
Line 1, column 10: is not defined in XML (only < > & " ' are). Use a character reference like   instead
Unquoted attribute
<item qty=2>Mug</item>
Line 1, column 11: Attribute values must be quoted: qty="…"
Two root elements
<a>1</a>
<b>2</b>
Line 2, column 1: A document can only have one root element; found a second one: <b>
XML only has five built-in entities: <, >, &,
" and '. HTML entities like and
© are errors unless a DTD defines them; use numeric references such as  
instead.
Format XML on the command line
# xmllint (libxml2, preinstalled on macOS and most Linux)
xmllint --format file.xml
XMLLINT_INDENT=' ' xmllint --format file.xml # 4 spaces
# Check well-formedness only
xmllint --noout file.xml
# Validate against a schema
xmllint --noout --schema schema.xsd file.xml
import xml.dom.minidom
print(xml.dom.minidom.parse("file.xml").toprettyxml(indent=" "))
Note that toprettyxml keeps the whitespace already between elements, so running it on an indented file
adds blank lines. To work with the data instead of the markup, convert it with XML to
JSON.
Frequently asked questions
How do I format XML?
Paste the XML into the formatter above. It is checked and pretty-printed as you type, with every element on its own line and nested elements indented. Choose 2 spaces, 4 spaces or tabs, or Minify to remove whitespace between tags. Copy the result or download it as a file.
Does formatting change my data?
No. Only whitespace between elements is changed. Text content, CDATA sections, comments, attribute values and the order of everything stay exactly the same. Elements that mix text and child elements, like <p>Hello <b>world</b></p>, are kept on one line because adding line breaks there would change the text.
What does “not well-formed” mean?
Well-formed XML follows the basic syntax rules: one root element, every tag closed and properly nested, attribute values quoted, and & and < escaped in text. Any XML parser rejects a document that breaks these rules. The formatter shows the line, column and reason for the first problem it finds.
Does it validate against an XSD or DTD?
No. It checks well-formedness, which is what every parser requires. Validation against a schema (XSD, DTD or RELAX NG) checks that the right elements and attributes are used, and needs the schema file and a tool such as xmllint --schema.
Can I format SVG, RSS, plist or Maven pom.xml files?
Yes. They are all XML, so you can paste or drop them in directly: SVG images, RSS and Atom feeds, Android layouts, Maven pom.xml, .NET .csproj files, macOS plist files, SOAP messages and sitemaps.