What is an XML Formatter?
An XML formatter (also called an XML beautifier or pretty printer) takes XML that is hard to read, such as a single-line API response, a minified config file or a document produced by a library, and rewrites it with consistent line breaks and indentation so the structure is obvious at a glance. A validator checks the same document against the syntax rules of XML and tells you exactly where it breaks.
XML (eXtensible Markup Language) is a W3C standard from 1998 for structured data and documents. It remains everywhere in software: SOAP web services, Maven pom.xml, Android manifests and layouts, Spring and .NET configuration, RSS and Atom feeds, sitemaps, SVG graphics, and the Office Open XML files inside every .docx and .xlsx. Most of it is written by machines and read by people, which is why a fast formatter is worth having open.
This tool uses your browser’s own XML parser, so the result matches what a real XML processor sees. Nothing is uploaded.
How to Use This XML Formatter
- Paste your XML into the input area.
- Pick an indent in the settings: 2 spaces, 4 spaces or tabs. Tick Remove comments if you want them gone.
- Click one of the actions:
- Format (
Ctrl+Enter): pretty-prints the document, one element per line. - Validate: checks well-formedness and shows the root element, element and attribute counts, maximum depth and declared namespaces.
- Minify: removes indentation for the smallest equivalent file.
- Format (
- Copy the result with the Copy button or
Ctrl+Shift+C.Ctrl+Lclears the workspace.
When the XML is broken, every action reports the line and column of the first error and shows the offending line with a ^ under the position. Line numbers match the input box, even if you pasted leading blank lines.
What the Formatter Changes, and What It Leaves Alone
A formatter must never change what your document means. In XML, whitespace inside text is data, so the rules here are deliberately conservative:
- Indentation between elements is rewritten. If an element contains only child elements (and whitespace), that whitespace is treated as layout and replaced with fresh indentation.
- Text is never touched. An element that contains any text, such as
<title> Hello </title>or mixed content like<p>Hello <b>world</b>!</p>, is written on one line exactly as it was. Many formatters break mixed content onto separate lines, which silently adds spaces to the text. xml:space="preserve"is honored. Everything inside such an element is copied verbatim.- CDATA sections stay intact, including any
<or&inside them, and the element holding them is not re-indented. - The prolog survives. The
<?xml version="1.0" encoding="UTF-8"?>declaration, the DOCTYPE (with its internal subset), comments and processing instructions such as<?xml-stylesheet?>are kept in their original order. - Escaping is correct. The parser decodes
<,&and". The formatter re-escapes them on output, so1 < 2stays1 < 2instead of becoming broken markup. - Empty elements become self-closing.
<item></item>is written as<item/>, which every XML parser treats as identical.
One case is ambiguous in XML itself. In <p><b>a</b> <i>b</i></p> the space between the two tags is whitespace-only, so it is treated as indentation. If that space matters, add xml:space="preserve" to the parent. That is what the attribute exists for.
Well-Formed vs Valid XML
These two words get mixed up constantly, and the difference decides which tool you need.
Well-formed means the document follows XML’s syntax rules: exactly one root element, every start tag closed (or self-closed), tags nested without overlapping, attribute values quoted, no duplicate attributes on one element, and < and & escaped in text. A document that isn’t well-formed isn’t XML at all, and every parser rejects it. This is what the Validate button checks.
Valid means well-formed and conforming to a schema that defines which elements and attributes are allowed, in what order and with what types. The common schema languages are:
| Schema language | Typical use | Check it with |
|---|---|---|
| DTD | Legacy documents, XHTML 1.x, DocBook | xmllint --valid |
| XSD (XML Schema) | SOAP/WSDL, enterprise integrations, Maven POM | xmllint --schema file.xsd |
| RELAX NG | Document formats such as ODF and DocBook 5 | xmllint --relaxng, Jing |
| Schematron | Business rules on top of a grammar | Saxon, xmllint |
A typical workflow is to format and fix syntax here first, then run schema validation in your build or IDE.
Common XML Errors and How to Fix Them
The exact wording depends on the browser (Chrome and Safari use libxml2 messages, Firefox uses expat-style ones), but the causes are always the same:
- Mismatched tag. Chrome: Opening and ending tag mismatch; Firefox: mismatched tag.
<a><b></a></b>overlaps. Close the inner element first:<a><b></b></a>. The reported line is often where the parser noticed the problem, so check the element that opens just before it. - Unescaped ampersand. Chrome: xmlParseEntityRef: no name or EntityRef: expecting ‘;’; Firefox: not well-formed. A bare
&in text or in a URL query string (?a=1&b=2) must be written&. Use CDATA for blocks of text full of special characters. - Undefined entity. Entity ’nbsp’ not defined. XML only knows
&,<,>,"and'. Replace HTML entities with numeric references such as , or paste the real UTF-8 character. The HTML Entity Encoder/Decoder helps convert them. - More than one root element. Chrome: Extra content at the end of the document; Firefox: junk after document element. Two top-level elements, often from concatenating two files or log lines, need a wrapper element.
- Declaration not at the start. XML declaration allowed only at the start of the document. Nothing, not even a blank line or a stray byte from a bad copy, may come before
<?xml ...?>. This tool trims leading whitespace for you, but your real file must not have it. - Unbound namespace prefix. Chrome: Namespace prefix soap on Envelope is not defined; Firefox: prefix not bound to a namespace. A prefix like
soap:needs anxmlns:soap="..."declaration on that element or an ancestor. This often happens when you copy a fragment out of a larger document. - Unquoted or duplicate attributes.
<a id=5>and<a id="1" id="2">are both errors. Quote every value and keep attribute names unique per element.
XML Namespaces in Practice
Namespaces let one document mix vocabularies without name clashes, for example a SOAP envelope wrapping your payload, or SVG embedded in XHTML. A declaration like xmlns:m="urn:example:orders" binds the prefix m to a URI, and xmlns="..." sets the default namespace for unprefixed elements. The URI is only an identifier. Nothing is downloaded from it. The formatter keeps every declaration where you wrote it, and Validate lists the namespace URIs it found, which quickly reveals a typo that would make XPath or XSLT silently match nothing.
XML vs JSON
| Feature | XML | JSON |
|---|---|---|
| Verbosity | Closing tags repeat names | Compact |
| Schema languages | DTD, XSD, RELAX NG | JSON Schema |
| Namespaces | Yes | No |
| Comments | Yes | No |
| Attributes vs content | Both | Only key/value pairs |
| Mixed text and markup | Native | Awkward |
| Data types | Text, unless a schema adds types | String, number, boolean, null |
XML still wins for documents (text with markup inside), for strongly schema-governed integrations, and wherever an existing ecosystem depends on it. JSON is the default for web APIs. If you’re working with JSON, use the JSON Formatter; for configuration files, the YAML Formatter.
Formatting XML on the Command Line
For files in a repository or a CI pipeline, these produce output similar to this tool:
xmllint --format input.xml > pretty.xml # libxml2, preinstalled on macOS
xmllint --noout input.xml # well-formedness check only
xmlstarlet fo --indent-spaces 4 input.xml # XMLStarlet
python3 -c "import sys,xml.dom.minidom as m; print(m.parse(sys.argv[1]).toprettyxml())" input.xml
Note that Python’s toprettyxml() splits mixed content onto separate lines (<p>Hello <b>w</b>!</p> becomes three indented lines), which changes the text. That’s fine for a quick look but not for files you commit. The browser tool is the fastest option for a snippet copied from a log, a network tab or an API response.