How to format XML

XML is strict: one unclosed tag breaks the whole document. Formatting it means laying the structure out with indentation — and checking, in the same pass, that the document is actually valid.

Formatting and validation go together

XML is stricter: every tag must close

XML is stricter than HTML: every tag must close, attributes must be quoted, and there must be exactly one root element. A formatter for XML therefore does two jobs. First it checks the document with a real XML parser, which is the same strict parser a server or an importer would use. If the document fails, you get the parser's error message instead of a guess. Only if it passes does the formatting step run.

One element per line, text kept compact

The formatting itself is straightforward: one element per line, children indented under their parent, and elements that hold only text kept on a single line so the output stays compact. Attributes stay on the opening tag, in their original order. Comments and CDATA sections are preserved, and the XML declaration at the top of the file is kept.

The errors that break XML most often

Unclosed tags and mismatched case

Most invalid XML fails for a small handful of reasons. An unclosed tag is the classic one — XML has no void elements, so every opening tag needs its close. Mismatched case is another: <Title> closed by </title> is an error, because XML tag names are case-sensitive. Unescaped ampersands and angle brackets inside text are a third — a bare & in a product name must be written as &amp;, and a < in a comparison as &lt;.

Two roots, or markup outside the root

Two structural errors round out the list: more than one root element, and text or markup sitting outside the root. The parser stops at the first problem it meets, so a large file often hides two or three errors — fix the reported one, format again, and repeat until the document passes cleanly.

XML versus JSON: when each fits

JSON has taken over most new APIs, but XML still runs a large part of the working world: configuration files, sitemaps, RSS feeds, office documents, SOAP services, and data feeds between companies. XML's strengths are the ones JSON lacks — comments, attributes, mixed content, and a schema language for validation — which is why it survives wherever documents, not just data, need to move.

The choice is rarely yours to make: the system you integrate with has already chosen. What you can choose is how you work with the format you are given, and formatting plus validation is the fastest way to understand an XML document you did not write.

Format XML in the browser

  1. Open the XML Formatter and paste your document into the Input box.
  2. Choose 2 or 4 spaces for indentation, then click Format and Validate.
  3. If the document is valid, the formatted output appears with an element count. If it is not, the parser error is shown plainly.
  4. Fix the reported error in the original file and format again — the parser stops at the first problem, so a large file may hide more than one.
  5. Click Copy output or Download to save the formatted document.

Reading parser errors

Parser errors look cryptic until you know their shape. They usually name the problem — an unclosed tag, an unexpected character — and give a line number. Go to that line in your original file, not the formatted output, and read the tags around it. The error is usually one line above or below the reported position, because the parser only notices the problem when it reaches the next thing that does not fit.

The most confusing errors come from unescaped characters. An ampersand in text produces an error about an entity reference, and a stray angle bracket produces an error about a malformed tag. When the message mentions entities or markup where you expected plain text, look for a bare & or < in the text near the reported line.

Keeping XML readable in a team

XML files tend to grow: a configuration file gains settings, a feed gains fields, and before long nobody can say what the structure is. Formatting on a schedule — or better, automatically, when the file is saved — keeps the structure visible and the diffs clean. Most editors can format XML on save with an extension.

The same habit applies to generated XML. A feed or export produced by code should be formatted before it is committed or shared, even if machines read it in production. The person debugging it at midnight will be grateful.

Related guides

Questions people ask

What does well-formed mean?

It means the document follows the basic XML rules: one root element, every tag closed, matching case, quoted attributes and no stray text outside the root. Well-formed is the minimum a parser accepts.

Does formatting check my schema?

No. This tool checks that the document is well-formed XML, not that it matches a particular schema, DTD or expected structure. Schema validation needs a dedicated validator.

Why does one error hide the next?

A parser stops at the first problem it meets, because everything after it may be misread. Fix the reported error, format again, and repeat until the document passes cleanly.