XML Formatter & Validator

Format, validate and beautify XML

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

  1. Paste your XML into the input area.
  2. Pick an indent in the settings: 2 spaces, 4 spaces or tabs. Tick Remove comments if you want them gone.
  3. 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.
  4. Copy the result with the Copy button or Ctrl+Shift+C. Ctrl+L clears 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 &lt;, &amp; and &quot;. The formatter re-escapes them on output, so 1 &lt; 2 stays 1 &lt; 2 instead 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 languageTypical useCheck it with
DTDLegacy documents, XHTML 1.x, DocBookxmllint --valid
XSD (XML Schema)SOAP/WSDL, enterprise integrations, Maven POMxmllint --schema file.xsd
RELAX NGDocument formats such as ODF and DocBook 5xmllint --relaxng, Jing
SchematronBusiness rules on top of a grammarSaxon, 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 &amp;. Use CDATA for blocks of text full of special characters.
  • Undefined entity. Entity ’nbsp’ not defined. XML only knows &amp;, &lt;, &gt;, &quot; and &apos;. Replace HTML entities with numeric references such as &#160;, 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 an xmlns: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

FeatureXMLJSON
VerbosityClosing tags repeat namesCompact
Schema languagesDTD, XSD, RELAX NGJSON Schema
NamespacesYesNo
CommentsYesNo
Attributes vs contentBothOnly key/value pairs
Mixed text and markupNativeAwkward
Data typesText, unless a schema adds typesString, 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.

Frequently Asked Questions

How do I format XML online?

Paste your XML into the input field and click Format (or press Ctrl+Enter). The document is parsed in your browser and written back out with one element per line, indented with 2 spaces, 4 spaces or tabs. The XML declaration, DOCTYPE, comments, CDATA sections and processing instructions are kept.

How do I validate XML?

Paste the XML and click Validate. If the document is well-formed you get a summary with the root element, element and attribute counts, nesting depth and declared namespaces. If it is not, you get the line and column of the first error, the parser's message and the offending line with a caret under the problem.

What is the difference between well-formed and valid XML?

Well-formed XML follows the syntax rules of the XML specification: one root element, every tag closed and properly nested, quoted attributes, escaped special characters. Valid XML is well-formed and also matches a schema such as a DTD, XSD or RELAX NG grammar. This tool checks well-formedness. Schema validation needs a schema-aware tool such as xmllint --schema.

Does formatting change my XML data?

No. The formatter only replaces whitespace between child elements, which is indentation. Elements that contain text are written exactly as they were, so mixed content like a paragraph with bold words keeps its spacing. Elements marked xml:space="preserve" and CDATA sections are never re-indented. Empty elements are written in the equivalent self-closing form, and entities are re-escaped where needed.

Why do I get "Entity 'nbsp' not defined"?

XML only predefines five entities: &amp;, &lt;, &gt;, &quot; and &apos;. HTML names such as &nbsp;, &copy; or &eacute; are errors unless a DTD declares them. Replace them with numeric character references (&#160; for a non-breaking space, &#169; for the copyright sign) or with the literal UTF-8 character.

How do I minify XML?

Click Minify. It removes the indentation between elements and keeps everything else, including spaces inside text and mixed content, so the minified file means exactly the same thing. Tick Remove comments in the settings to drop comments as well.

Is my XML data safe?

Yes. Parsing and formatting run entirely in your browser using its built-in XML parser. Your XML is never uploaded to a server, and the tool does not fetch external DTDs or entities referenced by the document.