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:

InputOutputWhy 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 &quot;hi&quot;"/>Attributes are always rewritten in double quotes
<!DOCTYPE note [<!ENTITY co "Example Ltd">]><note>&co; &amp; partners</note>The same text, on two lines when formattedThe internal subset and the entity reference are kept as written
<p>Hello <b>world</b>, again</p>Unchanged in both modesText 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.

InputOutputWhat happened
caf&#233; &#x2603; inside an elementcafé ☃Character references are written as the characters they stand for. In the same test &#160; became an invisible no-break space and &#9; a tab; only &#13; stayed a reference.
<t>a &lt; b &amp;&amp; c &gt; d, &quot;q&quot; &apos;s&apos;</t><t>a &lt; b &amp;&amp; c &gt; 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 &#10;, 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 goneThe 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.

InputChrome 156 and Chromium 153Firefox 155WebKit
<root><a></root>error on line 1 at column 16: Unexpected closing tag: root != aXML Parsing Error: mismatched tag. Expected: </a>. ... Line Number 1, Column 12error 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 10error on line 1 at column 10: xmlParseEntityRef: no name
Four lines, with <item>2 left open on line 3error on line 4 at column 7: Unexpected closing tag: root != itemXML Parsing Error: mismatched tag. Expected: </item>. ... Line Number 4, Column 3error on line 4 at column 8: Opening and ending tag mismatch: item line 3 and root
<p>a&nbsp;b</p> (no DOCTYPE)error on line 1 at column 10: Unexpected entity: nbspXML Parsing Error: undefined entity ... Line Number 1, Column 5error 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 5error 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:

StepChrome 156Chromium 153Firefox 155WebKit build
Valid XML check, 1,000,187 characters (35,855 elements)50 ms47 ms31 ms44 ms
Format, same file120 ms113 ms111 ms74 ms
Minify of the formatted text133 ms129 ms142 ms104 ms
Valid XML check, 4,900,181 characters (174,952 elements)239 ms243 ms145 ms134 ms
Format, same file578 ms570 ms515 ms436 ms
Minify of the formatted textRefused: too largeRefused: too largeRefused: too largeRefused: too large
Valid XML check, 10,000,110 charactersRefused, 0 msRefused, 0 msRefused, 0 msRefused, 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

  1. 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.

  2. 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.

  3. 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 &amp; and &lt;.
  • 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 &#233; and the escapes &quot; and &apos; 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 &#233; 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 &amp; 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 &#233; 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. &#233; became é and &#x2603; became ☃, and &quot; and &apos; inside text became plain quotation marks. Inside text, only &#13; 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.

Related Tools