Home / Developer tools / XML formatter

XML Formatter

Paste XML and get it back clean and indented, or minified into one line. Mixed content, CDATA and xml:space="preserve" are left exactly as you wrote them.

Layout

Formatted XML

What an XML formatter does

A formatter changes the layout of an XML document and leaves its content alone. Elements are indented by depth, each element starts on its own line, long tags are wrapped, and empty elements are written in one style. The data and the structure stay the same.

Before and after

<order id="42"><customer>Alex</customer><items><item sku="A1" qty="2"/><item sku="B7" qty="1"/></items></order>
<order id="42">
  <customer>Alex</customer>
  <items>
    <item sku="A1" qty="2"/>
    <item sku="B7" qty="1"/>
  </items>
</order>

Whitespace in XML

XML does not say which spaces matter, so a formatter has to be careful. This one sorts elements into three kinds:

  • Element-only content: an element whose children are other elements, such as items above. The spaces between the children are not data, so they are replaced by the indentation.
  • Text-only content: an element with just text, such as customer. It stays on one line, and you can choose to trim the spaces around the text.
  • Mixed content: text and elements together, such as a paragraph with an em tag inside. Here every space can matter, so the content is copied exactly as it is.

An element with xml:space="preserve" is also copied exactly. CDATA sections, comments, processing instructions and the DOCTYPE are kept.

Options

  • Indent: 2 spaces, 4 spaces or a tab.
  • Line width and attributes: wrap a start tag that gets too long, with one attribute per line, or never wrap.
  • Empty elements: keep them as written, or write all of them as <a/> or as <a></a>.
  • Attribute quotes: keep them, or use double quotes.
  • Sort attributes alphabetically, with namespace declarations first.
  • XML declaration: keep it, add one if it is missing, or remove it.
  • Remove comments and trim text.
  • Minify: write everything on one line and drop the spaces between elements.

It checks your XML first

Formatting needs well-formed XML. If your document has an error, such as a tag that is not closed or an ampersand that is not escaped, the tool shows every error it finds with the line and column and a marked line of code. Fix them, and the formatted result appears. The XML validator goes into more detail, and it can fix bare ampersands for you.

Private and free

Everything runs in your browser. Your XML is not uploaded.

Other pages in this family: the XML beautifier for minified or one-line XML, the XML minifier, the XML validator and the XML formatting guide. Convert your data with XML to JSON and JSON to XML, compare the formats in the JSON vs XML guide, and learn the rules in the XML syntax guide. For HTML, use the HTML formatter, which treats tags and spaces differently.

Frequently asked questions

How do I format XML?

Paste your XML in the box. The formatted result appears at once, and you can copy it or download it.

Does formatting change my data?

The structure and the content do not change. Spaces between elements are replaced by indentation, and mixed content is copied exactly. You can choose whether the spaces around text in simple elements are trimmed.

What happens if my XML is not valid?

The tool lists every error with its line and column and does not format until the XML is well-formed.

Can I minify XML?

Yes. Tick Minify to write the whole document on one line without the spaces between elements.