We use cookies

We use cookies and similar technologies to enhance your browsing experience, analyze site traffic, and personalize content and ads. By clicking "Accept", you consent to our use of cookies. Learn more in our Privacy Policy.

</>

XML Formatter

by Cosmovex

Indent:
XML Input
XML Output
Output will appear here

Free XML Formatter, Minifier, Validator & Converter

Format and beautify XML, minify XML, validate XML syntax, convert XML to JSON, and run XPath queries. No signup required.

More Developer Tools

View all tools →
{ }
JSON Formatter
Format, validate & explore JSON
YAML Formatter
Lint, format & convert YAML
CSV Formatter
View & format CSV tables
SQL Formatter
Format & highlight SQL queries
M↓
Markdown Preview
Live Markdown rendering
64
Base64
Encode & decode Base64 strings
JWT
JWT Decoder
Decode & inspect JWT tokens
%20
URL Encoder
Encode & decode URLs

The XML Formatter & Validator takes raw XML and pretty-prints it with consistent indentation, so a single-line config dump or a wall of unbroken markup becomes a readable, nested tree. It also checks the document for well-formedness: it confirms that every tag is closed, that elements nest correctly, that attribute quoting is valid, and that the document has a single root. If something is off, it points you at the problem instead of failing silently.

It runs entirely in your browser. Paste or drop a file in, and the parsing, formatting and validation all happen locally on your machine. Nothing is sent to a server, which matters when the XML is an API response with a session token, a SOAP envelope, a build manifest, or anything else you would rather not upload. There is no account, no rate limit, and you can keep working with the page open offline once it has loaded.

How it works and what it does

Paste XML into the input, drop a file, or type directly. The tool parses the markup into a node tree and re-serializes it, which is what produces clean output rather than a naive find-and-replace on angle brackets. Core actions:

  • Beautify / indent — re-emit the document with one level of indentation per nesting depth. You can usually pick 2 spaces, 4 spaces, or tabs.
  • Minify — strip insignificant whitespace between elements to shrink the payload for transport or storage.
  • Validate (well-formedness) — verify the document follows XML syntax rules: matched tags, correct nesting, a single root element, properly quoted attributes, and legal entity references.
  • Error reporting — when parsing fails, surface the location and reason (for example, a mismatched closing tag or an unescaped &) so you can jump straight to it.

Because it works on the parsed tree, the formatter preserves attribute values, CDATA sections, comments, and the XML declaration rather than mangling them.

A worked example

Say an API returns this on one line:

<order id="42"><item sku="A1">Pen</item><item sku="B2">Notebook</item></order>

Beautifying it with 2-space indentation gives you something you can actually read:

<order id="42">
  <item sku="A1">Pen</item>
  <item sku="B2">Notebook</item>
</order>

Now drop a closing tag:

<order id="42">
  <item sku="A1">Pen
  <item sku="B2">Notebook</item>
</order>

Validation flags it: the parser expected </item> but found <item>, so the first item is never closed. The fix is to add the missing </item>. This is the most common class of XML bug, and seeing the document indented makes the unbalanced nesting obvious at a glance.

Common use cases

  • Reading API responses — REST and especially SOAP services often return compact, single-line XML. Formatting it makes the structure legible while you debug.
  • Editing config filespom.xml, Android AndroidManifest.xml, .csproj, web config, and similar files are easier to review and diff when consistently indented.
  • Cleaning up generated output — exporters and serializers frequently emit XML with no whitespace at all. Beautify before committing so the file is reviewable.
  • Preparing payloads — minify a hand-formatted request body before sending it, or shrink stored XML to save space.
  • Quick sanity checks — paste a fragment to confirm it is well-formed before pasting it into a larger document or test fixture.
  • Learning and teaching — the indented view makes the parent/child relationship between elements explicit, which helps when you are getting familiar with a new schema.

Tips and gotchas

A few things that trip people up:

  • Well-formed is not the same as valid against a schema. This tool checks XML syntax (well-formedness). It does not check your document against a DTD or XSD, so it will not tell you that an element is in the wrong place per a schema, only that the markup itself is structurally legal.
  • Reserved characters must be escaped in text. A raw & is an error; write &amp;. The same goes for < and > inside element content, which become &lt; and &gt;.
  • Use CDATA for embedded markup or code. If an element needs to contain literal < or & (for example, an HTML snippet), wrap it in <![CDATA[ ... ]]> so the parser treats it as raw text.
  • There must be exactly one root element. Two top-level siblings will fail validation. Wrap them in a single parent if you need both.
  • Comments and the XML declaration are preserved, so formatting a real file won't strip your <?xml ... ?> line or your <!-- notes -->.

Well-formed vs. valid: what the parser actually checks

XML has two distinct correctness levels, and it helps to know which one you are getting.

Well-formedness is the baseline every XML document must meet. The rules are purely structural: one root element, every start tag has a matching end tag (or is self-closing like <br/>), elements nest without overlapping, attribute values are quoted, attribute names are unique within an element, and special characters are escaped. A document that breaks any of these cannot be parsed at all. That is what this tool verifies.

Validity is a stronger, optional guarantee: the document is well-formed and it conforms to a grammar declared in a DTD or XML Schema (XSD) — correct element order, required attributes, allowed data types, and so on. Checking validity requires the schema, which lives outside the document.

In practice, well-formedness catches the overwhelming majority of everyday XML mistakes (typos, unclosed tags, stray ampersands). Reach for a schema validator only when you need to enforce a specific contract.

Tips

  • Beautify a minified API response first, then read it — unbalanced tags become obvious once the document is indented.
  • Escape `&` as `&amp;` in text content; an unescaped ampersand is one of the most common parse errors.
  • Wrap embedded HTML, code, or anything with literal `<` and `&` in a `<![CDATA[ ... ]]>` block.
  • Minify before sending or storing a payload to cut size; beautify before committing to source control so diffs are readable.
  • Pick the indentation (2 spaces, 4 spaces, or tabs) that matches the surrounding project so reformatted files don't create noisy diffs.
  • If a file with sensitive data needs formatting, this tool keeps it on your machine — nothing is uploaded.

How to use XML Formatter & Validator

  1. 1Paste your XML.
  2. 2It's checked for well-formedness as you type.
  3. 3Format with proper indentation or minify it.
  4. 4Copy the clean XML — no data is sent anywhere.

Frequently asked questions

Does this validate XML against an XSD or DTD schema?

No. It checks well-formedness (correct XML syntax: matched tags, proper nesting, quoting, a single root). It does not validate against a DTD or XSD, which would require you to supply that schema.

Is my XML uploaded to a server?

No. Parsing, formatting, and validation all run locally in your browser. The XML never leaves your machine, so it's safe to paste responses that contain tokens or private data.

Why do I get an error on a single `&` in my text?

`&` starts an entity reference in XML, so a bare ampersand is invalid. Replace it with `&amp;`. Likewise use `&lt;` for `<` and `&gt;` for `>` inside element content.

What does "multiple root elements" mean?

An XML document must have exactly one top-level element. If you have two or more elements at the outermost level, wrap them in a single parent element to make it well-formed.

What's the difference between beautify and minify?

Beautify adds line breaks and indentation so the document is human-readable. Minify removes insignificant whitespace between elements to make the payload smaller for transport or storage.

Will formatting change my data or strip comments?

No. It re-indents the markup but preserves attribute values, text content, CDATA sections, comments, and the XML declaration. Only whitespace between elements changes.

Can I format a self-closing tag like `<img/>`?

Yes. Self-closing (empty) elements such as `<br/>` or `<item id="1"/>` are valid XML and are preserved as-is during formatting.

How does it handle namespaces and prefixes like `<ns:item>`?

Prefixed element and attribute names are treated as ordinary names for well-formedness, so namespaced documents format and validate fine. It checks syntax, not whether each prefix is bound to a declared namespace.

← All toolsRead our guides →