XML Formatter
Paste XML to format with indentation for readability.
Back to all tools on ToolForge
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:
- Parse: Input text is parsed into a DOM tree using the browser's DOMParser
- Serialize: The DOM tree is serialized back to a string with XMLSerializer
- 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 |
|---|---|---|
| < | < | Less-than sign |
| > | > | Greater-than sign |
| & | & | Ampersand |
| " | " | Double quote |
| ' | ' | Apostrophe |
Well-Formed vs Valid XML
- Well-formed: Follows XML syntax rules (proper nesting, closed tags, quoted attributes, valid characters)
- Valid: Well-formed AND conforms to a DTD or XML Schema (XSD)
- This tool: Checks for well-formedness only, not schema validation
Common XML Errors
- Unclosed tags: <element> without matching </element>
- Mismatched tags: Opening <foo> but closing </bar>
- Improper nesting: <a><b></a></b> (crossed tags)
- Unescaped ampersand: Using & instead of & in text
- Multiple root elements: XML must have exactly one root element
- Invalid characters: Control characters not allowed in XML
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.