XML Formatter

Paste XML to format with indentation for readability.

Back to all tools on ToolForge

More in Developer Tools

XML



Formatted

About XML Formatter

This XML formatter indents XML so nested elements are easier to read and inspect. It parses minified or poorly-formatted XML and produces consistently indented output with proper line breaks between elements.

It is useful for feeds, config files, SOAP payloads, RSS/Atom feeds, and legacy integrations when raw XML arrives as one long hard-to-read block. The formatter also validates XML syntax and reports parsing errors.

XML Formatting Process

The formatter uses a three-step process:

  1. Parse: Input text is parsed into a DOM tree using the browser's DOMParser
  2. Serialize: The DOM tree is serialized back to a string with XMLSerializer
  3. Indent: Line breaks are added between tags, and indentation is applied based on nesting depth
// Formatting algorithm:
1. Parse XML string → DOM Document
2. Serialize DOM → String with tags separated
3. For each line:
   - If closing tag: decrease indent
   - Apply indent (2 spaces × depth)
   - If opening tag: increase indent

Common XML Use Cases

Format Description Common Use
RSS 2.0 Really Simple Syndication Blog feeds, podcasts
Atom IETF standard feed format Web syndication
SOAP Simple Object Access Protocol Web service messages
SVG Scalable Vector Graphics Vector images, icons
XML Schema XSD definition files XML structure validation

XML Special Characters

Five characters must be escaped in XML:

Character Entity Name
< &lt; Less-than sign
> &gt; Greater-than sign
& &amp; Ampersand
" &quot; Double quote
' &apos; Apostrophe

Well-Formed vs Valid XML

Common XML Errors

XML Namespaces

Namespaces prevent element name conflicts in XML:

<root xmlns:books="http://example.com/books"
      xmlns:music="http://example.com/music">
  <books:title>Book Title</books:title>
  <music:title>Song Title</music:title>
</root>

The formatter preserves namespace declarations and prefixes exactly as they appear in the input.

Frequently Asked Questions

What is XML formatting and why is it useful?
XML formatting (pretty printing) adds indentation and line breaks to XML documents, making nested structures visually clear. Minified XML compresses everything into one line to save space, but formatted XML is essential for reading, debugging, and manual editing of configuration files, feeds, and API responses.
How does XML indentation work?
The formatter parses XML into a DOM tree, then serializes it back with line breaks between tags. Opening tags increase the indent level, closing tags decrease it. Each nested level receives consistent indentation (typically 2 spaces), making parent-child relationships obvious.
What makes XML invalid?
Common XML errors include: unclosed tags, mismatched tag names, improperly nested tags, unescaped special characters (<, >, &, ", '), missing root element, multiple root elements, and invalid characters. The formatter validates XML and reports parsing errors.
Does formatting change the XML content?
Formatting adds whitespace (indentation and line breaks) but does not change element names, attribute values, or text content. However, whitespace-only text nodes may be normalized. For XML where whitespace is significant (like some document formats), verify the output matches expectations.
What is the difference between well-formed and valid XML?
Well-formed XML follows syntax rules (proper nesting, closed tags, quoted attributes). Valid XML is well-formed AND conforms to a DTD or XML Schema definition. This formatter checks for well-formedness but does not validate against schemas or DTDs.
How are XML namespaces handled?
Namespaces are preserved during formatting. The formatter maintains namespace declarations (xmlns attributes) and prefixed element names. Namespace prefixes are not normalized or changed—whatever prefixes appear in the input remain in the formatted output.