Free tool · Developer tools
XML formatter
Paste XML or open a file to pretty-print it with clean indentation, squeeze it onto one line, or check that it is well-formed. A mistake comes back with its line, column and the fix, and the file never leaves your device.
- Free
- No sign-up
- Runs in your browser
15 linesRoot <catalog>, formatted
- Well-formed XML 1.0
- 2-space indent
- LF line ends
- 1 comment kept
- xml:space="preserve" kept on 1 element
- 1 mixed-content element left as written
- 1 namespace declaration
- Size
- 411 B → 462 B
- Lines
- 2 → 15
- Elements
- 11 (7 attributes)
- Depth
- 4 levels
How to use the XML Formatter
- Paste into Your XML, or pick a file under Or choose an XML file. The file is read on your device.
- Under What to do choose Format, Minify or Check only.
- For Format, set the Indent (2 spaces, 4 spaces or a tab) and the Line endings. Tick Remove comments if you do not want them in the result.
- Press Copy or Download. If the XML is broken, read the message and press Show the error in the box to jump to the spot.
How do I format XML?
Paste the XML and leave Format selected: the result appears as you type, with every child element on its own line and pushed in one indent step per level. Copy it or download it as a .xml file. The formatter only moves whitespace between tags; it never touches the text inside an element or the value of an attribute.
The example already in the box is a two-line product catalog. Formatted with 2 spaces it becomes 15 lines and grows from 411 to 462 bytes, because the indentation is real characters. It holds 11 elements and goes 4 levels deep, which the Elements and Depth lines in the result show. Switch to Minify and the same data shrinks to 410 bytes on a single line, so minify(format(x)) gives back the original layout minus the one line break after the declaration.
Choose 2 spaces for config files and feeds you read in a browser, 4 spaces if your team style asks for it, and a tab if you want each reader to pick the width in their own editor. The table shows what each choice costs on the example.
| Setting | Lines | Size (UTF-8) |
|---|---|---|
| Format, 2 spaces | 15 | 462 bytes |
| Format, 4 spaces | 15 | 500 bytes |
| Format, tab | 15 | 443 bytes |
| Format, 2 spaces, CRLF | 15 | 476 bytes |
| Minify | 1 | 410 bytes |
| Minify, comments removed | 1 | 385 bytes |
CRLF adds one byte per line break. Choose it only when a program that reads the file insists on it; XML parsers themselves treat both endings the same.
What does well-formed XML mean?
Well-formed XML follows the grammar of the XML 1.0 Recommendation from the W3C, and any parser must refuse a file that breaks it. The core rules are short: exactly one root element holds everything else; every start tag has a matching end tag, or closes itself with />; tags close in the reverse order they opened; names start with a letter, an underscore or a colon; each attribute appears once per tag and its value sits in quotes; and the characters < and & appear in text only as part of markup or an entity reference.
Valid is a stricter word. A valid document is well-formed and also matches a document type definition (DTD) that it names. This tool checks well-formedness only. It reads a DOCTYPE so that entities declared inside it are accepted, but it does not compare the elements against the DTD or against an XML Schema (XSD). Use a schema-aware validator from your build tools for that step.
The specification calls a broken rule a fatal error: the parser must report it and stop processing the document normally. That is why one stray ampersand can blank out a whole feed. The checker here stops at the first problem as well, then tells you where it is. Each row below is a real run of this tool on a small broken snippet.
| Snippet | Problem | Message from the tool |
|---|---|---|
<p>Fish & chips</p> | A bare ampersand | Line 1, column 9: A bare "&" in the text. Write & for an ampersand, or finish the reference with ";" (for example <). |
<p>Price list</p> | An HTML entity | Line 1, column 9: The entity is not defined. XML knows only < > & ' and " unless a DOCTYPE declares more. Use a character reference instead, such as   for a no-break space. |
<list> <item>One </list> | A tag left open | Line 3, column 1: Found </list> but the open element is <item> (line 2). Close </item> first: tags must close in the reverse order they opened. |
<a><b></a></b> | Tags closed in the wrong order | Line 1, column 7: Found </a> but the open element is <b> (line 1). Close </b> first: tags must close in the reverse order they opened. |
<img src=photo.jpg/> | An attribute without quotes | Line 1, column 10: The value of "src" must be in quotes, found "p". Write src="..." or src='...'. |
<input checked/> | An attribute without a value | Line 1, column 15: The attribute "checked" has no value. In XML every attribute needs ="value" (for example checked="checked"). |
<item/><item/> | Two root elements | Line 1, column 8: A second root element. An XML file has exactly one root (here <item>); wrap both in one parent element. |
<?xml version="1.0"?><a/> | A blank line before the declaration | Line 2, column 1: The XML declaration (<?xml ...?>) may only appear once, at the very start of the file, with nothing before it (not even a space or a blank line). |
<a><!-- old -- new --></a> | Two hyphens in a comment | Line 1, column 13: Two hyphens in a row ("--") are not allowed inside a comment, and a comment cannot end with "--->". Use single hyphens or a space. |
Why are some lines left exactly as they were?
Some whitespace is data, so the formatter does not reflow it. Text in an element that holds only text, such as <price>12.50</price>, is written back character for character, leading and trailing spaces included. An element that mixes text with child elements, like a sentence with an <em> inside it, is also kept on one line as written, because adding a line break there would add a space to the sentence.
An element marked xml:space="preserve" keeps everything inside it untouched, and so does every element nested in it unless one of them switches back with xml:space="default". The specification defines this attribute so that a document can ask programs to keep its spacing, which matters for poetry, code samples and fixed-width text. CDATA sections, comments and processing instructions such as <?xml-stylesheet ...?> are copied exactly too.
Two small things do change. Spaces between attributes inside one tag become a single space, while the attributes keep their order and their own quote marks. Line breaks are read the way the specification asks a parser to read them, so CR LF and a lone CR both count as one LF before the result is written with the ending you pick.
How do I fix the most common XML errors?
Start at the line and column in the result, and look a little earlier too: the reported spot is where the parser noticed the problem, which can sit just after the real mistake. The fixes below cover most of what people paste.
A bare ampersand in text, as in a company name or a link with several query parameters, must be written &. Named entities from HTML such as or © do not exist in plain XML, which knows only five: <, >, &, ' and ". Use a numeric reference instead, such as   for a no-break space.
Habits from HTML cause the next group. In XML a <br> must be written <br/>, an attribute such as checked needs a value, and every value needs quotes. When the message says a tag is never closed, the line it gives is where that tag opened, so you can follow it down to the place the end tag belongs.
The famous error at line 1, column 1 usually means something sits before the first <: a blank line, a space, or stray text. The declaration <?xml version="1.0"?> must be the very first characters of the file when it is present. A UTF-8 byte order mark is fine and is skipped here. If the message names a control character, the file probably came from a copy and paste out of another program; delete that character. Once the check is green, the JSON formatter is the matching tool for API replies, and the HTML entities tool encodes text you want to drop into XML safely.
Use it with
- SQL Formatter: format SQL queries.
- JSON Escape: escape and unescape strings.
- JavaScript Beautifier: format or minify JS.
Questions
How do I pretty print XML?
Paste it above and keep Format selected. Every child element moves to its own line, indented 2 spaces, 4 spaces or a tab per level. The example in the box goes from 2 lines to 15.
What is the difference between well-formed and valid XML?
Well-formed means the file follows the XML grammar: one root, matching and properly nested tags, quoted attributes, legal entity references. Valid means it is well-formed and also matches the DTD it declares. This tool checks well-formedness, not DTD or XSD rules.
Why does it say the entity is not defined?
Because is an HTML entity. Plain XML has only <, >, &, ' and " unless a DOCTYPE declares more. Write   for a no-break space, or declare the entity in the DOCTYPE.
What does "content is not allowed in prolog" mean?
Something comes before the first < of the file, such as text, a blank line before the declaration, or a stray character from a copy and paste. Paste the file here and the message gives the line and column of the first character that should not be there.
Does formatting change my data?
No text or attribute value changes. Only whitespace between tags is added or removed, and elements with mixed text, xml:space="preserve" or CDATA are kept as written. Minify the formatted result and you get the original compact layout back.
Is my XML uploaded anywhere?
No. The check and the formatting run in this page with its own code, so nothing you paste or open is sent to a server. You can load the page, switch the connection off and keep formatting.
The tool's results are computed in your browser by the code on this page, using the sources listed above; the explanations are checked against the tool's own output.