XML Formatter and Beautifier

Paste XML to pretty-print it with clean indentation, check that it is well-formed, minify it, or explore it as a collapsible tree. Errors point to the exact line and column. Your XML never leaves the browser.

Indent
View
Output

How to format and pretty-print XML online

  1. Paste the XML (or drop in the contents of a .xml, .svg, .config or .xsd file).
  2. Choose the indent: two spaces, four spaces or a tab.
  3. Press Format XML. Each element goes on its own line, nested one indent deeper than its parent; an element that only holds short text stays on one line (<price>9.99</price>).
  4. Switch to Collapsible tree to fold large sections, or press Minify to strip whitespace for transport.

The formatter is a streaming tokenizer, so it keeps everything a DOM round-trip would lose: the <?xml ... ?> declaration, a custom DOCTYPE with an internal subset, comments, CDATA sections, processing instructions and the original attribute order and quoting.

Validate XML and find the error line

Before formatting, the document is checked with the browser’s own XML parser (the same DOMParser engine that loads SVG and RSS). If it is not well-formed, you get the parser’s message plus the line and column, and Go to error jumps to it in the input. The most frequent causes:

Parser messageCauseFix
Opening and ending tag mismatchA tag is closed with a different name or case, or never closedXML is case-sensitive: <Item> must close with </Item>; empty elements need <br/>
EntityRef: expecting ‘;’ / xmlParseEntityRef: no nameA bare & in text or an attribute (often in URLs)Write &amp;, or wrap the text in <![CDATA[ ... ]]>
Unescaped ‘<’ not allowed in attributesA literal < inside an attribute valueUse &lt;
AttValue: " or ' expectedAn unquoted attribute such as id=1Quote every value: id="1"
Attribute redefinedThe same attribute twice on one elementRemove the duplicate
Extra content at the end of the documentMore than one root element, or text after the rootWrap everything in a single root element
XML declaration allowed only at the startWhitespace, a BOM or a second <?xml?> before the declarationMake <?xml the very first characters

Well-formed means the syntax rules above are met. Valid means it also matches a DTD or XSD schema — that needs the schema, for example xmllint --noout --schema schema.xsd file.xml. If you need a schema to start from, the XML to XSD converter infers one from a sample.

XML format rules in one table

RuleCorrectWrong
Exactly one root element<orders><order/><order/></orders><order/><order/>
Proper nesting<b><i>x</i></b><b><i>x</b></i>
Quoted attributes<item id="7"/><item id=7/>
Escape the five specials in text&amp; &lt; &gt; &quot; &apos;AT&T
Names start with a letter or underscore<_2fa/><2fa/>
Case-sensitive<Note></Note><Note></note>

Need to escape a block of text for use inside XML? Use the XML escape / unescape tool.

XML syntax highlighting and tree view

Formatted output is colour-highlighted: element names, attribute names, attribute values, comments, CDATA and the declaration each get their own colour, which makes a misplaced quote or a stray closing tag easy to spot. Turn highlighting off for very large files or when you only need plain text to copy. The Collapsible tree view lets you fold whole branches of a large document — a SOAP body, a Maven <dependencies> block, an RSS <item> list — and shows how many children each element has. For a read-only inspector with search, the XML viewer is also available.

XML minifier: compress XML to one line

Minifying removes the whitespace-only text between tags (and, optionally, comments) so the payload is smaller for SOAP calls, message queues or embedding in JSON. Text inside elements, CDATA and attribute values are untouched. Gzip already compresses indentation well, so minify when a size limit is strict or when the XML is going inside another string.

Format XML in code or on the command line

Command line (libxml2)

xmllint --format messy.xml > pretty.xml        # pretty-print
XMLLINT_INDENT=$'\t' xmllint --format messy.xml  # tab indent
xmllint --noout messy.xml                        # well-formedness check only

Python

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

JavaScript (browser)

const doc = new DOMParser().parseFromString(xml, "application/xml");
const err = doc.querySelector("parsererror");
if (err) console.error(err.textContent);   // contains line and column

Java

Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
t.transform(new StreamSource(new StringReader(xml)), new StreamResult(System.out));

C# / .NET

var doc = System.Xml.Linq.XDocument.Parse(xml);
Console.WriteLine(doc.ToString());   // indented by default

Common questions

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

Well-formed XML follows the syntax rules: one root, closed and properly nested tags, quoted attributes, escaped special characters. Valid XML is also checked against a DTD or XSD schema. This formatter checks well-formedness; schema validation needs the schema, for example xmllint --schema.

Does formatting change my XML data?

Only whitespace: indentation between tags and the leading or trailing spaces around text. Names, attributes, text, comments, CDATA, the declaration and the DOCTYPE are kept as written. If a document relies on significant whitespace (xml:space="preserve" or mixed content), check those elements afterwards.

How do I find where my XML is broken?

Press Format XML or Validate only. The error panel shows the parser message with the line and column of the first problem, and Go to error selects that spot in the input.

Is my XML uploaded to a server?

No. Formatting, minifying and validation all run in your browser. Nothing is sent over the network, so it is safe for configuration files and SOAP payloads.

What indent should I use for XML?

There is no standard. Two spaces is common for configuration (Maven, Android), four spaces or tabs for hand-written documents. Match what your repository already uses so diffs stay small.

Are CDATA sections, comments and the XML declaration kept?

Yes. Pretty-printing keeps <![CDATA[ ... ]]> blocks, comments, processing instructions, the <?xml ... ?> declaration and a custom DOCTYPE exactly as written. Minify removes comments only if you untick Keep comments.

Can I format XML files that are several megabytes?

Yes. The formatter is a single-pass tokenizer and handles multi-megabyte files in well under a second. The tree view is capped for very large documents so the page stays responsive.