XML Formatter & Beautifier

XML Input
1
Formatted XML
1

Last updated:

Jsonic's XML formatter (also called an XML beautifier or pretty printer) re-indents any well-formed XML so its structure is readable: one element per line, children indented, short text-only elements kept on a single line. It first checks the document with the browser's XML parser and reports the first error if the XML is not well-formed. Comments, CDATA, processing instructions and the DOCTYPE are preserved. Minify does the reverse. Everything runs in your browser — your XML is never uploaded.

What the formatter does to your XML

Formatting changes only the whitespace between tags. Given a single-line document like this:

<?xml version="1.0"?><catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer's Guide</title><discontinued/></book></catalog>

you get:

<?xml version="1.0"?>
<catalog>
  <book id="bk101">
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <discontinued/>
  </book>
</catalog>
  • Each element starts on its own line, indented one level deeper than its parent.
  • Elements that contain only a short piece of text stay on one line.
  • Attributes stay in the order you wrote them; extra spaces between attributes are collapsed.
  • Comments, <![CDATA[ ... ]]> sections, <?xml ... ?> declarations and <!DOCTYPE> are kept verbatim.

Common reasons XML will not format

A formatter can only indent XML that parses. These are the errors people hit most often:

  • Unclosed or mismatched tags — <b><i>text</b></i> closes in the wrong order.
  • An unescaped ampersand — write &amp; instead of a bare & in text and attribute values.
  • More than one root element — wrap sibling top-level elements in a single parent.
  • Unquoted attribute values — id=5 must be id="5".
  • Text before the XML declaration — <?xml ... ?> must be the very first thing in the file.

Format XML from the command line, Python or VS Code

# Linux / macOS (libxml2)
xmllint --format input.xml > pretty.xml
# Python standard library
from xml.dom import minidom
print(minidom.parseString(open("input.xml").read()).toprettyxml(indent="  "))
// Node.js — prettier with @prettier/plugin-xml
npx prettier --plugin=@prettier/plugin-xml --write input.xml

In VS Code, install Red Hat's XML extension and run Format Document. For a one-off file, pasting it here is faster.

Working with XML and JSON

Need the data as JSON instead? Use the XML to JSON converter, or go the other way with JSON to XML. For JSON itself, the JSON formatter does the same job as this page.

How to format XML

  1. Paste your XML into the left panel (or click Example).
  2. Choose 2 spaces, 4 spaces or Tab indentation.
  3. Click Format — or Minify to remove whitespace.
  4. Copy the result or click Download to save formatted.xml.

FAQ

How do I format XML online?

Paste your XML into the input panel and click Format. Each element goes on its own line, children are indented by the amount you pick (2 spaces, 4 spaces or a tab), and short text-only elements such as <title>Guide</title> stay on one line. Copy the result or download it as formatted.xml.

What is the difference between an XML formatter and an XML beautifier?

Nothing — both names mean the same thing: re-indenting XML so the nesting is readable. "Pretty print" is a third name for it. The opposite operation is minifying, which strips the whitespace between tags to make the file smaller.

Does formatting change my XML data?

No. Only the whitespace between tags changes. Element names, attributes and their values, text content, comments, CDATA sections, processing instructions and the DOCTYPE are kept exactly as written. Line breaks inside attribute values and inside text are preserved.

Why does the formatter say my XML is invalid?

XML must be well-formed: every opening tag needs a matching closing tag (or must be self-closing like <br/>), tags must nest correctly, attribute values must be quoted, there must be exactly one root element, and characters like & and < in text must be escaped as &amp; and &lt; (or wrapped in CDATA). The error message shows the first problem the browser's XML parser found.

How do I minify XML?

Click Minify. Whitespace between tags is removed, runs of whitespace inside text collapse to a single space, and comments are dropped. CDATA sections and the XML declaration are kept.

Is my XML uploaded to a server?

No. Validation uses the browser's built-in DOMParser and formatting runs in JavaScript on your device, so private or internal XML (SOAP payloads, config files, sitemaps) never leaves your computer.

How do I format XML in code or in an editor?

Command line: xmllint --format file.xml. Python: xml.dom.minidom.parseString(s).toprettyxml(indent=" "). Java: a Transformer with OutputKeys.INDENT set to "yes". VS Code: install the "XML" extension by Red Hat, then run Format Document (Shift+Alt+F on Windows, Shift+Option+F on macOS).