XML guide

How to format XML online

Formatting XML makes its nested structure visible without changing the meaning of the document. This guide covers beautifying XML, what well-formedness means, and what to check when a parser reports an error.

Format a minified XML document

  1. Open the free XML formatter.
  2. Paste the document or upload the file from your device.
  3. Choose two spaces, four spaces, or tabs for indentation.
  4. Select Format XML.
  5. Copy, download, or move the clean result back into the input.

A formatter adds whitespace between elements to expose parent-child relationships. It should preserve element names, attributes, namespaces, text nodes, comments, CDATA and processing instructions exactly as written.

What “well-formed XML” means

Before XML can be formatted safely it must follow the core XML 1.0 syntax rules. Every opening element needs a matching close in the correct nesting order, attribute values must be quoted, reserved characters must be escaped when used as text, and the document must have exactly one root element.

Malformed: <order><item>Book</order> — theitem element is never closed and order closes out of order.

Well-formed:<order><item>Book</item></order>

Common XML parsing errors

  • Mismatched tags: an element closes with a different name or in the wrong nesting order.
  • Unescaped characters: text contains a raw ampersand or less-than sign instead of an XML entity. Use the XML escape tool to fix this safely.
  • Unquoted attributes: an attribute value is missing its quotation marks. XML, unlike HTML, never permits this.
  • Multiple root elements: two top-level elements appear without a single containing root.
  • Invalid namespace prefixes: a prefixed element is used without a corresponding namespace declaration in scope.

XML validation versus formatting

Well-formedness validation checks basic XML syntax. Schema validation goes further by comparing the document with rules in an XSD or DTD — which elements may appear where, what data types values must have, which combinations are required. Those questions can only be answered when you supply the schema.

The XML validator here checks well-formedness. It does not claim a document satisfies a particular business schema, because without that schema no tool honestly can.

Choosing indentation

Two spaces produce compact output and work well for deeply nested API responses. Four spaces create stronger visual separation for shallow documents. Tabs let each reader control visual width in their own editor. The best choice is whatever convention your project already uses — consistency matters far more than the specific width.

Formatting SOAP, RSS, SVG and namespaced XML

SOAP envelopes and enterprise documents use namespace prefixes such as soap: andns1:. These can be formatted like any other XML, but namespace declarations must remain in scope and must never be removed to shorten output — a prefix that falls out of scope changes the meaning of every element beneath it.

RSS and Atom feeds contain repeated entries that become far easier to scan once indented. SVG stores vector graphics as XML and formats identically. XSD schemas are themselves XML documents; the W3C XMLSchema.xsd is roughly 88 KB and expands to about 2,100 formatted lines without difficulty.

Converting between XML and other formats

XML has attributes, namespaces, ordered mixed content and comments. JSON and YAML have none of those, so any conversion is lossy in one direction. TheXML to JSON converter documents exactly which convention it uses and what it discards, rather than converting silently. The reverse direction,JSON to XML, has to solve a different problem: JSON permits multiple top-level values and keys that are not valid element names.

Ready to clean up your XML?

Use the free browser-based tool — no sign-up, and the document never leaves your machine.

Open the XML formatter