XML Formatter
Published Updated
Indent XML with two spaces, squeeze it onto one line, or just check that it is well-formed. The check reruns as you type, and in Chrome its error message gives the line and column.
Well-formed is what gets checked
The green Valid XML line means the text passed the browser's own XML parser. In the terms of W3C XML 1.0, that is a well-formed document. Valid is a stricter word in the specification: it needs a document type declaration and conformance to it, and this tool never fetches a DTD or a schema.
Parsing uses DOMParser.parseFromString with the application/xml type. On broken input it does not throw; it returns a document holding a parsererror element, and the page shows that element's message. Google Chrome 156 reports <root><a></root> as error on line 1 at column 16: Unexpected closing tag: root != a. Other browsers word the message their own way.
What Format and Minify rewrite
Measured with the page's own formatting code in Chrome 156:
| Input | Output | Why it matters |
|---|---|---|
<item> value </item> | <item> value </item> | Text inside an element is data, so its spaces stay |
<e></e> | <e/> | Same meaning: XML 1.0 section 3.1 allows either form for an empty element |
<a t='say "hi"'/> | <a t="say "hi""/> | Attributes are always rewritten in double quotes |
<!DOCTYPE note [<!ENTITY co "Example Ltd">]><note>&co; & partners</note> | The same text, on two lines when formatted | The internal subset and the entity reference are kept as written |
<p>Hello <b>world</b>, again</p> | Unchanged in both modes | Text mixed with tags stays on one line, because a line break there would be new text |
Comments, CDATA sections, processing instructions, namespaces and the <?xml ... ?> declaration all come through. The declaration is copied from your original text, since the parser does not keep it.
What came back changed, character by character
We ran the page's own formatting code (the functions behind Format, Minify and the Valid XML line) on short generated documents in Chrome 156, Chromium 153 (Playwright build, not Google Chrome), Firefox 155 and Playwright's WebKit build (not the Safari app), and compared each result with its input. Every formatted result parsed again without an error, and formatting it a second time changed nothing. All four engines gave the same output in each row unless the row says otherwise. The Output column shows the Minify result so that it fits on one line; Format gives the same text with line breaks and two-space indents.
| Input | Output | What happened |
|---|---|---|
café ☃ inside an element | café ☃ | Character references are written as the characters they stand for. In the same test   became an invisible no-break space and 	 a tab; only stayed a reference. |
<t>a < b && c > d, "q" 's'</t> | <t>a < b && c > d, "q" 's'</t> | The escapes for <, & and > stay. Quotation marks and apostrophes in text come out as plain characters. |
<root><a> </a><b> + line break + </b><c></c><d /></root> | <root><a/><b/><c/><d/></root> | Content made only of spaces or line breaks is dropped, so the element comes out empty. Text with any other character keeps its spaces: <a> x </a> stayed as written. |
<a v="one + line break + two"/> | <a v="one two"/> | A line break typed inside an attribute came back as a space in all four engines. Written as the reference , it was kept. |
<r xmlns="urn:a" xmlns:x="urn:x" xmlns:unused="urn:u"> | Chrome 156 and Chromium 153: xmlns, xmlns:unused, xmlns:x. Firefox 155 and the WebKit build: the original order. | Namespace declarations were reordered by the two Chromium-based engines only. Ordinary attributes kept their order in all four, and the unused declaration was kept. |
<?xml-stylesheet type="text/xsl" href="style.xsl"?><root/> | The stylesheet instruction once, then <root/> | Only <?xml followed by a space or a line break is read as the declaration, so <?xml-stylesheet ...?> and <?xml-model ...?> at the top are kept once, and a second Format changed nothing. With <?xml version="1.0"?> in front, the declaration stayed first. |
<pre xml:space="preserve"> <x/> </pre><n> <y/> </n> | <pre xml:space="preserve"> <x/> </pre><n><y/></n> | Spacing under xml:space="preserve" is left alone; the same spacing elsewhere is layout and goes. |
A blank line before <?xml version="1.0"?> | The declaration first, blank line gone | The status line showed an error in all four engines, since the declaration has to come first, yet Format and Minify trim the text before parsing and succeeded. |
Same mistake, four different error messages
The red line under the box is the browser's own parser message, with boilerplate text removed. We fed the same broken inputs to Chrome 156, Chromium 153, Firefox 155 and the WebKit build. The messages are quoted as the page showed them; in Firefox 155 the part shown as ... is the address of the page.
| Input | Chrome 156 and Chromium 153 | Firefox 155 | WebKit |
|---|---|---|---|
<root><a></root> | error on line 1 at column 16: Unexpected closing tag: root != a | XML Parsing Error: mismatched tag. Expected: </a>. ... Line Number 1, Column 12 | error on line 1 at column 17: Opening and ending tag mismatch: a line 1 and root |
<p>fish & chips</p> | error on line 1 at column 10: Unexpected token inside entity: | XML Parsing Error: not well-formed ... Line Number 1, Column 10 | error on line 1 at column 10: xmlParseEntityRef: no name |
Four lines, with <item>2 left open on line 3 | error on line 4 at column 7: Unexpected closing tag: root != item | XML Parsing Error: mismatched tag. Expected: </item>. ... Line Number 4, Column 3 | error on line 4 at column 8: Opening and ending tag mismatch: item line 3 and root |
<p>a b</p> (no DOCTYPE) | error on line 1 at column 10: Unexpected entity: nbsp | XML Parsing Error: undefined entity ... Line Number 1, Column 5 | error on line 1 at column 11: Entity 'nbsp' not defined |
<a/><b/> | error on line 1 at column 5: Unexpected token: < | XML Parsing Error: junk after document element ... Line Number 1, Column 5 | error on line 1 at column 5: Extra content at the end of the document |
Firefox 155 also prints the faulty line with a caret under the column. The line number is where the parser noticed the problem, which can be later than the mistake: for the <item> left open on line 3, all four engines pointed at line 4, where </root> closes, and only the WebKit build's message mentions line 3. For a mismatched closing tag, Chrome 156 and Chromium 153 counted the column to the tag's last character, Firefox 155 to the start of the tag name and the WebKit build one character past the end.
Timing a 1 MB and a 4.9 MB file
For speed we generated catalogue-style XML with a seeded script (records with attributes, nested tags and short text, ASCII only, so characters and bytes are equal) and timed each step once in Chrome 156, Chromium 153, Firefox 155 and the WebKit build:
| Step | Chrome 156 | Chromium 153 | Firefox 155 | WebKit build |
|---|---|---|---|---|
| Valid XML check, 1,000,187 characters (35,855 elements) | 50 ms | 47 ms | 31 ms | 44 ms |
| Format, same file | 120 ms | 113 ms | 111 ms | 74 ms |
| Minify of the formatted text | 133 ms | 129 ms | 142 ms | 104 ms |
| Valid XML check, 4,900,181 characters (174,952 elements) | 239 ms | 243 ms | 145 ms | 134 ms |
| Format, same file | 578 ms | 570 ms | 515 ms | 436 ms |
| Minify of the formatted text | Refused: too large | Refused: too large | Refused: too large | Refused: too large |
| Valid XML check, 10,000,110 characters | Refused, 0 ms | Refused, 0 ms | Refused, 0 ms | Refused, 0 ms |
The page repeats the Valid XML check on every change to the box, so with the 4.9 MB file each edit cost 0.13 to 0.24 seconds of checking in our test. Format adds indentation: the 1 MB file grew by 23 percent to 1,230,679 characters on 46,101 lines, and Minify turned that back into text identical to the input. The 4.9 MB file grew to 6,024,868 characters, past the 5,000,000-character limit, so Minify then answered This XML is too large to process. Near the limit, minify the original rather than a formatted copy.
The other limits behaved as stated. In all four engines 501 nested elements formatted and 502 gave This XML is too deeply nested to format (more than 500 levels deep). A root with 199,999 empty children formatted, and one more child gave This XML has too many elements to format (more than 200,000). The Valid XML line stayed green in both cases, because only Format and Minify apply these limits. We did not test files with multi-byte text.
Tidying an XML file
Step 1
Paste or upload
Paste into the box or drop an .xml file. The status line under the box turns green or shows the parser's error.
Step 2
Format or Minify
Either button rewrites the text in the same box. Format indents each level by two spaces; Minify removes the whitespace between tags.
Step 3
Copy the result
Copy to Clipboard takes the whole box; there is no download button.
Errors seen most often
- A bare & or <. XML 1.0 section 2.4 says both must not appear in their literal form in text; write
&and<. - Two root elements.
<a/><b/>fails at the second tag; wrap everything in one outer element. - A closing tag in the wrong order. The message names both tags, as in root != a above.
- A copied fragment. A snippet cut from the middle of a larger file usually lacks its outer element or its namespace declarations.
To turn the cleaned file into JSON, use XML to JSON; for JSON going the other way, JSON to XML; for JSON on its own, JSON Formatter.
XML formatting questions
Does Valid XML mean my file matches its schema?
No. It means the browser's XML parser read the text without a syntax error, which the XML specification calls well-formed. No DTD or XML Schema is loaded, so a missing required element or a wrong data type still gets the green line.
Will formatting change my data?
Mostly the layout. Text with any visible character keeps its spaces, an element that mixes text and tags stays on one line, and nothing under xml:space="preserve" is touched. In our test a few things did change: an element holding only spaces or a line break came out empty, as <a/>; character references such as é and the escapes " and ' in text came out as the characters themselves; and attributes were rewritten in double quotes.
Does my DOCTYPE keep its entity declarations?
Yes. The DOCTYPE is copied from your text as written, with any internal subset in square brackets, and a reference such as &co; stays a reference in both modes. Character references such as é are a separate case: they came back as the characters they stand for.
Why does an ampersand break my XML?
A bare & starts an entity reference, so the parser expects a name and a semicolon after it. Write & instead, or wrap the text in a CDATA section. In Chrome, fish & chips inside an element gives error on line 1 at column 10: Unexpected token inside entity.
Can I indent with four spaces or tabs?
No. The indent is fixed at two spaces per level. Most editors can re-indent a pasted file if your project uses another width.
How big a file can it handle?
Up to 5,000,000 characters, 500 levels of nesting, 200,000 nodes and 10,000 attributes on one element. Past any of those it shows a message naming the limit, and files over 50 MB are refused before they are read.
Is an xml-stylesheet line at the top kept as it is?
Yes, once. A file that began with <?xml-stylesheet ...?> and had no <?xml ...?> declaration came back with the instruction written once, in all four browser engines we tried, and <?xml-model ...?> did the same; formatting the result again changed nothing. With <?xml version="1.0"?> as the first line, the declaration stayed first and the instruction followed it.
Why did é turn into é after formatting?
The formatter writes back what the browser's parser read, and the parser hands over a character reference as the character itself. é became é and ☃ became ☃, and " and ' inside text became plain quotation marks. Inside text, only and the escapes for <, > and & were written as references. Keep a copy of the original if another program expects the references.
Why do Firefox and Chrome show different errors for the same XML?
The message comes from each browser's own XML parser. For <root><a></root> Chrome 156 said error on line 1 at column 16: Unexpected closing tag: root != a, while Firefox 155 reported XML Parsing Error: mismatched tag. Expected: </a>. with Column 12 and the page address. The line can also point past the real mistake, at the tag where the parser noticed it.
Why can't I minify the file I just formatted?
Formatting adds indentation, about 23 percent more characters in our test, and both buttons refuse text over 5,000,000 characters. A 4,900,181-character file formatted fine but grew to 6,024,868 characters, and Minify then answered This XML is too large to process. Minify the original file instead.