XML Formatter
Pretty-print or minify XML, with error positions.
About XML Formatter
An XML formatter checks that a document is well-formed, then pretty-prints it with a chosen indent or minifies it to one line. Errors show the line and column.
How to use
- Paste XML or load a
.xmlfile. The output updates as you type. - Choose 2 spaces, 4 spaces or Tab to pretty-print, or Minify to remove the whitespace between elements.
- Fix any error shown under the input: it names the line, column and reason, as the browser's XML parser reports it.
- Copy the result or download it as
.xml, named after the loaded file.
What it does
- Parses with the browser's own XML parser, so the well-formedness rules are the real ones: matching tags, quoted attributes, a single root element, declared entities, correct nesting.
- Pretty-prints one element per line, nested elements indented one level deeper. An element that contains text, or text mixed with inline elements, stays on one line with its whitespace runs collapsed to a single space.
- Keeps the markup as written: attribute order and quote style, the XML declaration, DOCTYPE, comments, processing instructions, CDATA sections and entity references such as
&are all preserved, not rewritten. - Minify removes whitespace-only text between elements and collapses runs of whitespace inside text to one space. Comments and CDATA are kept.
- Does not validate against a DTD or XML Schema, and does not treat
xml:space="preserve"specially.
Example
<note><to>Ada</to><body>Hello <b>there</b></body></note>
→
<note>
<to>Ada</to>
<body>Hello <b>there</b></body>
</note>
Privacy
Parsing and formatting run in the browser. No request is made, and nothing is stored.
Frequently asked questions
- What does "well-formed" XML mean?
- That the document follows the XML syntax rules: one root element, every start tag closed, tags properly nested, attribute values quoted, and the characters <, & escaped in text. Well-formed is different from valid, which means conforming to a particular DTD or schema.
- Why does my XML fail with "mismatched tag"?
- An element was closed with a different name than it was opened with, or a tag in between was never closed. XML is case-sensitive, so <Item> and </item> do not match. The error shows the line and column of the closing tag the parser did not expect.
- Is whitespace significant in XML?
- Whitespace inside text content is passed to the application as-is, so it can matter. Whitespace between elements is usually ignored by consumers, which is why pretty-printing and minifying are safe for most documents. Documents that rely on exact spacing should not be reformatted.
- What is a CDATA section?
- A block of text, written <![CDATA[ ... ]]>, in which < and & are taken literally instead of starting markup or an entity. It is handy for embedding code or HTML in XML. This formatter leaves CDATA sections exactly as written.