XML Formatter
Validate XML for well-formedness and pretty-print it with consistent indentation, or minify it.
How to use this tool
Steps for using the XML Formatter.
- Paste your XML into the Input XML panel, click Upload file (XML, SVG, RSS, XSD, config files — read locally), or click Sample. Tags, attributes, values, comments and CDATA are colour-coded as you type.
- Pick an action: Format to pretty-print, Minify to remove whitespace between tags, or To JSON to convert the document (attributes become
@namekeys). The output updates automatically. - Set the indentation (2 spaces, 4 spaces or tabs) and toggle Remove comments or Self-close empty tags (
<tags></tags>→<tags/>). - If the document is not well-formed, the status bar shows the line and column with an excerpt; the line is highlighted and Go to line jumps to it.
- Switch the output to Tree to browse the element structure with attributes and text — click any element to jump to it in the input. The status bar counts elements, attributes, depth, comments and namespaces.
- Copy, Download or Expand the result.
Example
Pretty-print a note with a comment and CDATA
<?xml version="1.0"?><note priority="high"><to>Ada</to><from>Grace</from><!-- draft --><body><![CDATA[Use <b>bold</b> sparingly]]></body></note><?xml version="1.0"?>
<note priority="high">
<to>Ada</to>
<from>Grace</from>
<!-- draft -->
<body><![CDATA[Use <b>bold</b> sparingly]]></body>
</note>Common use cases
- Reading SOAP requests and responses captured from logs or API clients.
- Checking RSS or Atom feeds and XML sitemaps for well-formedness errors before publishing.
- Tidying Maven
pom.xml, Android layout or .NET config files for review. - Shrinking XML payloads before embedding them in tests or messages.
- Locating the exact line of a mismatched tag in a large generated document.
How XML formatting and validation work
The document is first checked for well-formedness with the validator from fast-xml-parser: every start tag needs a matching end tag, attributes must be quoted and unique, and tags must nest properly. The tool also requires exactly one root element and no stray text outside it, as the XML specification does. Errors are reported with a line and column so you can jump straight to the problem.
Well-formed XML is then pretty-printed by a small formatter that works on the original tokens rather than a parsed object, which is why nothing is lost: the XML declaration, processing instructions such as <?xml-stylesheet?>, <!DOCTYPE> declarations with internal subsets, comments, CDATA sections, self-closing tags and the exact text of every attribute are written back as they were. Elements that only contain text stay on one line, as in <name>Keyboard</name>, with surrounding whitespace trimmed.
Whitespace can be meaningful in XML, so the formatter is careful with it. Elements with mixed content — text interleaved with child elements, like <p>Ships in <em>2–3</em> days</p> — are kept exactly as written, and so is anything inside an element marked xml:space="preserve". Only whitespace between tags in element-only content is changed.
Minify removes that insignificant whitespace (and, unless you keep them, comments) to produce the smallest equivalent document. Text inside text-only and mixed-content elements is kept byte-for-byte, and CDATA sections are always preserved because they are part of the data. This is a well-formedness check, not schema validation: the tool does not check documents against an XSD or DTD.
Limitations
- Checks well-formedness only — no XSD, DTD or Relax NG schema validation, and external entities are never loaded.
- Mixed-content elements are kept on one line exactly as written rather than re-indented.
- Attribute order and quoting are preserved, not normalised.
- Input is limited to 2 million characters.
Frequently asked questions
Does formatting change my data?
Only whitespace between tags in element-only content changes, plus trimming around text in text-only elements. Mixed content, xml:space="preserve" blocks, CDATA, comments and attributes are kept as written.
Can it validate against an XSD schema?
No. It checks that the XML is well-formed (correct syntax and nesting). Schema validation requires the schema file and a full validating parser.
Why is my document with two top-level elements rejected?
An XML document must have exactly one root element. Wrap the elements in a single parent, or format each fragment separately.
Can I convert XML to JSON here?
Not in this tool. If you already have JSON, the JSON Formatter formats and validates it the same way.
Is my XML sent to a server?
No. Validation, formatting and minifying all happen in your browser.