XML formatter

The XML formatter indents XML code for easy reading based on its element structure, minifies it to a smaller size, and checks that its structure is valid.
Input
Paste XML and click Format or Minify. Every action also checks that the XML is valid. Up to 1 MB.

Checking and formatting run in the browser. Please enable JavaScript.

Everything runs in your browser; nothing is sent or stored.

  • Paste your document into the "XML code" field, and click "Format" or "Minify".
  • Under "Indent", choose 2 spaces, 4 spaces, or Tab, and the document is formatted again with your choice.
  • Comments, CDATA sections, processing instructions, and attribute order are preserved. An element that contains text alongside other elements, or that has xml:space="preserve", is left as is, so its text does not change.
  • Below the result, the tool shows the number of elements in the document and the number of characters in the result.
  • When the XML is not valid, an error message appears instead of the result. An XML document with a DOCTYPE or ENTITY declaration is refused, to prevent external files from loading and entities from expanding.
  • Copy the result by clicking "Copy".

Formatting and minifying an XML document

When you format a document, each element appears on its own line, and the indentation of each line grows with the nesting level of the element, so you can easily see which elements sit inside other elements. When you minify a document, the spaces and line breaks between elements are removed, and the resulting document is smaller, which is useful, for example, for sending through an API, storing in a database field, or pasting into a configuration file. Both actions change only whitespace: the text, entities such as &amp;, the <?xml ...?> declaration, and comments stay as written. Extra spaces inside tags are removed, and an attribute value in single quotes is wrapped in double quotes in the result, without changing the meaning of the value.

Elements with mixed content

An element that contains text alongside other elements, for example <p>Hello <b>world</b></p>, is called an element with mixed content. In an element with mixed content, spaces and line breaks are part of the text, and adding them would change the text that the receiving software reads. That is why an element with mixed content is written in the result without adding or removing whitespace inside it. The same applies to an element that contains only text, an element that contains a CDATA section, and an element with the xml:space="preserve" attribute. Elements with mixed content are common in documents that combine text and formatting, such as XHTML documents.

What the validity check covers

A well-formed XML document follows fixed rules: it has a single root element, every start tag is closed by a matching end tag or written as an empty element such as <br/>, and elements are closed in the reverse order of opening. Attribute values are written in double or single quotes, and tag names are case-sensitive. Under the XML standard, the & and < characters in text are written as entities, and a named entity that is not one of the five entities predefined in XML must be declared in the document. A standard XML parser rejects a document that breaks any of these rules, and the validity check helps you find the error before you send the document to another system.

Common XML structure errors

  • <name>Hosting</Name>the end tag does not match the start tag, because tag names are case-sensitive.
  • <a><b></a></b>element b is closed after element a, instead of before it.
  • <price currency=USD>an attribute value must be in double or single quotes.
  • <a/><b/>an XML document has only one root element.
  • <p>VPS & backup</p>under the XML standard, the & character in text is written as &amp;.
  • <p>&nbsp;</p>the nbsp entity is defined in HTML, but not in XML.

XML formatter FAQ

Does the tool validate against a schema, such as XSD or DTD?

The tool checks that the document is well-formed, and does not validate it against a schema. A well-formed document can still be missing an element, or contain an element with the wrong name or a value that the schema does not allow. To validate against a schema, use a dedicated tool that also accepts the XSD file.

How do I find where the error is in the document?

When the location of the error can be found, the message shows the line number and the column number, and after you click "Format" or "Minify", the cursor moves to the location of the error in the field. Sometimes the actual error comes before the location shown, for example when a tag opened earlier in the document was not closed.

Why is there an error at the start of a document that looks valid?

Under the XML standard, the <?xml ...?> declaration must appear at the very start of the document, with no space, blank line, or text before it. This error is common when the code that generates the document adds a blank line or a space before the declaration. Delete the characters before the declaration and format again.

Why is a document with a DOCTYPE not accepted?

DOCTYPE and ENTITY declarations can define entities that point to external files (an XXE attack) or entities that expand into a huge amount of text. That is why the tool does not accept a document with a DOCTYPE or ENTITY declaration. When the document does not use the entities defined in it, you can remove the DOCTYPE line and format again.

Can I format HTML code with the tool?

You can format HTML code only when it is written according to XML rules, such as XHTML. Regular HTML code usually has tags without an end tag, such as <br>, and entities such as &nbsp; that are not defined in XML, so the validity check shows an error. To format regular HTML code, use the "HTML formatter" tool.

What is the maximum size of an XML file I can format?

The input can be up to 1 MB, and a nested structure is checked and formatted up to a depth of 100 levels.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement