XML

XML Formatter and Beautifier

Re-indents XML so its structure is readable, keeping text-only elements on one line so no whitespace enters a value.

Loading the tool…

Processing happens locally in your browser. What you paste or load is processed by this page and is not uploaded to a server. Nothing is stored unless you use a control that says it stores something, and you can clear anything this site has kept from the privacy page.

How to use this tool

  1. Paste your XML into the input box, or load an .xml file from your machine.
  2. Choose the indentation, and decide whether to keep the declaration and any comments.
  3. Select Format. If the document is not well-formed the error names the line and column.
  4. Copy the result, or download it as a file.

What xml formatter does

Formatting XML is less innocent than formatting JSON. In JSON, whitespace between tokens carries no meaning and reformatting is always safe. In XML, the whitespace inside an element is text, and a parser hands it to the application unless the schema or the reader has been told otherwise. Breaking <name>Ada</name> across three lines changes that element from the string "Ada" to a string with a newline and two spaces on either side of it.

This formatter therefore indents structure but never text. An element whose children are all text stays on a single line, exactly as it was, and only elements that contain other elements get broken and indented. Attributes stay on the opening tag, comments are kept in place, and CDATA sections are passed through byte for byte.

Frequently asked questions

It can, and that is why this formatter is careful. Whitespace inside an XML element is text, so an element holding only text is left on one line rather than broken and indented. Only elements that contain other elements are broken apart, and the whitespace added there sits between elements where no schema treats it as a value.

No. The document is parsed and re-printed by JavaScript running in your browser. Nothing you paste or load leaves your machine, and no request carrying your document is made.

CDATA sections are passed through exactly as they are, including their whitespace, because their whole purpose is to hold text a parser must not touch. Comments are kept by default and can be dropped with the comments option.