XML Formatter

Turn cramped XML into cleanly indented structure, or minify it to one line. Invalid syntax? It points you at the error.

Input XML (processed locally, never uploaded)
Result

The pieces of an XML document

ConstructLooks likePurpose
Declaration<?xml version="1.0"?>Optional; states version and encoding
Element<item>…</item>The main structure; may nest freely
Attribute<item id="7">Metadata on an element; cannot repeat, cannot nest
Text<name>Alice</name>Character data
CDATA<![CDATA[ raw < text ]]>A block where markup is treated as literal text
Comment<!-- note -->Ignored by parsers

An element is either empty (<tag/>) or has a matching closing tag. Unlike HTML, XML is strict: every tag must close, attribute values must be quoted, and tags are case-sensitive.

XML versus JSON

What must be escaped

Five characters are special: &, <, >, " and '. The first two are mandatory in all text; the quotes matter inside attributes. Wrapping content in CDATA avoids escaping entirely, but CDATA cannot appear inside an attribute value and carries a real cost: any embedded ]]> sequence terminates the block early.

Namespaces prevent collisions

Two vocabularies can both define a title element without conflicting, because each is bound to a namespace URI. Be aware that the prefix is only a local nickname — what actually identifies a namespace is the URI, and two different prefixes bound to the same URI are the same namespace. Comparing documents by prefix rather than by URI is a frequent and subtle bug.

Formatted output is a trade-off

Indenting XML makes it readable, but it inserts whitespace text nodes that some consumers will treat as content. For documents where whitespace is meaningful, parse the compact form and present the formatted version only for viewing.

Using CDATA without falling into its traps

CDATA marks a span where markup is not parsed, which is why it turns up in XML that embeds HTML, SQL or scripts. Three rules govern how it behaves:

For ordinary values, escaping is the safer default; CDATA earns its place only for large blocks of genuinely foreign markup where escaping every angle bracket would make the document unreadable.

Related tools

JSON formatter CSV ⇄ JSON HTML / CSS formatter

FAQ

Does this XML formatter upload my data?
No. All parsing, beautifying and minifying happen locally in your browser; the XML text is never sent to any server.
Why does the beautified XML gain an extra declaration line?
To make the result a standard XML file, the beautifier automatically prepends at the top. If you don't want it, just delete that line.
How do I read XML errors?
Click “Validate” and the tool checks the syntax with the browser's parser, then shows the error message in the status bar (most browsers include the approximate line number).