XML Formatter

Format, minify, and validate XML

Loading tool…

About XML Formatter & Minifier

XML Formatter & Minifier re-indents an XML document so element nesting is visible, strips insignificant whitespace when you need a compact payload, and reports where the markup stops being well-formed.

XML has one rule that trips up everyone coming from JSON: whitespace inside an element is content. Indenting a document with mixed content, such as a paragraph containing an inline tag, changes the text a parser will report. This is why a formatter must distinguish element-only content, where added indentation is harmless, from mixed content, where it is not.

How to use this developer tool

  1. Paste the document, SOAP envelope, RSS feed, or configuration file into the input area.
  2. Format to expand the nesting, or minify to remove whitespace that sits only between elements.
  3. If the document is rejected, read the reported line and check for an unclosed tag, a stray ampersand, or a mismatched case in a tag name.
  4. Review any element that mixes text and child elements, and confirm the formatter left its inner spacing alone.
  5. Copy the result, keeping the expanded version for review and the compact one for transport.

Worked example: a feed entry that will not parse

This fragment fails on a character that XML reserves. The formatter points at the entity reference, which is where a parser gives up.

Input
<item>
  <title>Q1 results: revenue & margin</title>
  <link>https://example.com/report?year=2026&period=q1</link>
</item>
Reported error
EntityRef: expecting ';' at line 2, column 34

A bare ampersand always starts an entity reference in XML. Both lines are affected: write &amp; in the title, and either escape the ampersand in the URL the same way or wrap the value in a CDATA section.

Characters that must be escaped

The first two are mandatory everywhere in content. The rest are mandatory only in the context named.

CharacterEntityRequired in
&&amp;All text content and attribute values
<&lt;All text content and attribute values
>&gt;Recommended everywhere; required after ]]
"&quot;Attribute values quoted with double quotes
'&apos;Attribute values quoted with single quotes

Troubleshooting

The document parses in one tool and fails in another.

Why it happens Some readers are lenient about undeclared namespace prefixes, duplicate attributes, or a missing XML declaration. A strict parser rejects all three.

Fix Format and validate with a strict parser first, then fix what it reports. Being accepted by a permissive reader is not evidence that the document is well-formed.

Text inside an element changes after formatting.

Why it happens The element holds mixed content, so the indentation the formatter added became part of the text node.

Fix Keep mixed-content elements on a single line, or wrap the literal text in CDATA so no formatter can rewrap it.

Non-ASCII characters appear as question marks or mojibake.

Why it happens The declared encoding does not match the bytes of the file, most often a UTF-8 file declared as ISO-8859-1.

Fix Set the encoding in the XML declaration to match how the file was actually written, and re-save it in that encoding.

A minified document breaks a downstream consumer.

Why it happens The consumer relied on whitespace inside an element that carries text, so removing it changed a value.

Fix Minify only documents whose elements contain either text or children, never both. When in doubt, ship the formatted version.

When this tool is the right choice

  • Reading a SOAP envelope or a vendor API response deeply enough to find one field.
  • Making a build or application configuration file reviewable before it is committed.
  • Confirming that generated markup is well-formed before handing it to a strict parser.
  • Compacting a document for transport where every byte is billed.

When to use something else

  • Checking a document against an XSD or DTD. Well-formedness and schema validity are different questions; use the XML Validator for the first and a schema processor for the second.
  • Editing HTML. HTML5 is not XML and tolerates unclosed tags that XML rejects; use the HTML Previewer instead.
  • Transforming a document into another shape. That is XSLT's job.

Privacy and data handling

The document is parsed and re-serialized in this browser tab. Nothing is uploaded, and no copy survives the tab being closed. XML configuration files frequently contain connection strings and credentials in plain attributes, so remove them before pasting a file exported from a running system.

Related developer tools

Frequently asked questions

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

Well-formed means the syntax is correct: tags nest properly, every element is closed, and reserved characters are escaped. Valid means the document also matches a declared schema such as an XSD or DTD. A document can be well-formed and still invalid.

Why does formatting sometimes change my content?

Because whitespace inside an element is content in XML. When an element contains both text and child elements, indentation added between them becomes part of the text. Formatters avoid touching such elements, but the safest option is CDATA.

Does the XML declaration have to be present?

It is optional when the document is UTF-8 and uses XML 1.0. Include it whenever the encoding is anything else, or whenever a consumer might guess the encoding wrongly.

How are namespaces handled?

Prefix declarations are preserved exactly as written. A formatter never rewrites or consolidates them, because a prefix bound to a different URI later in the document would change the meaning of every element that uses it.

Can I format an XML file with a DOCTYPE?

Yes, the internal subset is preserved. External entities referenced by the DOCTYPE are not fetched, which is also the correct security behavior: resolving them is the basis of XXE attacks.

Why is my attribute order different?

It should not be, and is not here. XML treats attribute order as insignificant, so some tools reorder them; that is safe for parsers but noisy in a diff, which is why order is preserved.