How to format XML
- Paste your XML, drop an
.xmlfile onto the editor or load it from a URL. - Click Format (or press Ctrl+Enter) and choose the indentation. Minify removes the whitespace between elements.
- If the XML is not well-formed, the error box shows the line, the column and the cause, and the line is highlighted. Fix it, then copy or download the result.
What the XML formatter does
- Pretty print – every element on its own line, indented with 2 spaces, 4 spaces or tabs.
- Keeps your data intact – text, attribute values and entities such as
&or are written exactly as in the source; only the whitespace between elements changes. - Respects mixed content – elements that mix text and markup, like
<p>Hello <b>world</b></p>, and elements withxml:space="preserve"are left untouched. - Keeps everything else – the XML declaration, DOCTYPE, comments, CDATA sections and processing instructions stay in place.
- Options – collapse empty elements to
<a/>, remove comments, or minify the whole document. - Statistics – size, number of elements and attributes, and nesting depth.
Common reasons XML is not well-formed
| Problem | Example | Fix |
|---|---|---|
| Tags in the wrong order | <b><i>text</b></i> | Close the inner element first: </i></b> |
| Unescaped ampersand | AT&T | Write AT&T |
| Unescaped less-than sign | a < b | Write a < b or use CDATA |
| Attribute without quotes | <item id=7> | <item id="7"> |
| Two root elements | <a/><b/> | Wrap them in one parent element |
| HTML entities | , © | Use  , © or declare them in a DTD |
Format XML on the command line and in code
- Linux and macOS:
xmllint --format file.xml - Python:
xml.dom.minidom.parseString(text).toprettyxml(indent=" ") - C#:
XDocument.Parse(text).ToString() - Java: an XSLT identity transform with
OutputKeys.INDENTset toyes - VS Code: the Format Document command with an XML extension installed
Why use Online XML Formatter?
- FreeEvery tool is free with no limits, accounts or watermarks.
- PrivateProcessing happens locally in your browser. Files are never uploaded.
- FastLightweight pages that load instantly on any device.
- Works everywhereUse it on Windows, macOS, Linux, Android and iPhone.
Frequently asked questions
Is my XML uploaded to a server?
No. Formatting and validation run in your browser. Only Load URL fetches the file through our server, because browsers block most direct downloads from other sites; the content is passed straight to you and not stored.
Does formatting change my data?
No. Only the whitespace between elements changes. Text, attribute values, entities, CDATA sections and comments stay exactly as written, and elements with mixed content are left untouched.
What is the difference between well-formed and valid XML?
Well-formed XML follows the syntax rules: one root element, properly nested and closed tags, quoted attribute values and escaped special characters. Valid XML additionally matches a schema (XSD) or a DTD. This tool checks well-formedness.
Can I format very large XML files?
Yes. A 2.6 MB file with 20,000 records is parsed and formatted in a fraction of a second, and the editor draws line numbers only for the visible lines, so it stays responsive.