X Xerobit

XML Formatter Online — Beautify and Validate XML Instantly

An XML formatter adds proper indentation to minified XML, making it human-readable. It also validates syntax — catching unclosed tags, missing attributes, and encoding issues...

Mian Ali Khalid · · 5 min read
Use the tool
XML Formatter
Format, validate, and beautify XML documents.
Open XML Formatter →

An XML formatter parses XML and outputs it with consistent indentation and line breaks. Minified XML is a single unbroken line — correct but unreadable. A formatter makes the hierarchy visible without changing any data.

Use the XML Formatter to beautify and validate XML directly in your browser.

What XML formatting does

XML has strict syntax rules. Formatting tools do two things simultaneously:

  1. Parse the document — if the XML is malformed (unclosed tag, invalid character reference), parsing fails and the tool reports the error with line/column number

  2. Reserialize with indentation — if parsing succeeds, the tool outputs the same document with consistent indentation

The result: you get validation (the formatter tells you if your XML is broken) plus readability in one operation.

Minified vs formatted XML

Minified (SOAP response from a real API):

<?xml version="1.0" encoding="UTF-8"?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:HelloResponse xmlns:ns2="http://ws.example.com/"><return>Hello World</return></ns2:HelloResponse></S:Body></S:Envelope>

Formatted:

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Body>
    <ns2:HelloResponse xmlns:ns2="http://ws.example.com/">
      <return>Hello World</return>
    </ns2:HelloResponse>
  </S:Body>
</S:Envelope>

The tree structure — envelope → body → response → return — is immediately visible in the formatted version.

Common XML errors the formatter catches

Unclosed tags

<!-- Invalid: <name> is never closed -->
<person>
  <name>Alice
  <age>30</age>
</person>

Error: Unclosed tag 'name' at line 2, column 3

Mismatched tags

<!-- Invalid: opened <b>, closed </i> -->
<p>Hello <b>World</i></p>

XML requires exact tag matching (unlike HTML, which is more lenient).

Invalid attribute syntax

<!-- Invalid: attribute value not quoted -->
<img src=photo.jpg>

<!-- Valid: -->
<img src="photo.jpg"/>

In XML, all attribute values must be quoted (single or double quotes).

Unescaped special characters

<!-- Invalid: bare & in content -->
<company>Smith & Jones</company>

<!-- Valid: escaped -->
<company>Smith &amp; Jones</company>
CharacterEscape
&&amp;
<&lt;
>&gt;
"&quot;
'&apos;

Encoding declaration mismatch

<?xml version="1.0" encoding="UTF-8"?>
<!-- The actual file must be saved in UTF-8 for this to be valid -->

If the XML declaration says UTF-8 but the file is actually Latin-1, parsers may reject it or produce corrupted output.

Formatting XML in code

Python

import xml.dom.minidom

# Parse and pretty-print:
xml_string = '<root><child>value</child></root>'
dom = xml.dom.minidom.parseString(xml_string)
pretty = dom.toprettyxml(indent='  ')
print(pretty)
# <?xml version="1.0" ?>
# <root>
#   <child>value</child>
# </root>

# From a file:
with open('input.xml') as f:
    dom = xml.dom.minidom.parse(f)
print(dom.toprettyxml(indent='  '))

Python with lxml (cleaner output)

from lxml import etree

tree = etree.parse('input.xml')
# Remove extra blank lines from minidom's output:
formatted = etree.tostring(tree, pretty_print=True, xml_declaration=True, encoding='UTF-8')
print(formatted.decode())

Command line

# xmllint (Linux/macOS, usually pre-installed):
xmllint --format input.xml

# xmllint from pipe:
cat input.xml | xmllint --format -

# Save to file:
xmllint --format input.xml > output.xml

JavaScript (Node.js)

const { DOMParser, XMLSerializer } = require('@xmldom/xmldom');
const format = require('xml-formatter');

const xml = '<root><child>value</child></root>';
const formatted = format(xml, { indentation: '  ', lineSeparator: '\n' });
console.log(formatted);

XML namespaces and why they look confusing

XML namespaces prevent naming conflicts when combining documents from different sources:

<root xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:Envelope>
    <item xsi:type="xsd:string">value</item>
  </soap:Envelope>
</root>

The xmlns:prefix="URI" declarations define namespace prefixes. The URI is just an identifier — it doesn’t have to be a URL that resolves to anything. The prefix (soap:, xsi:) is a shorthand for that URI.

Formatters preserve namespace declarations and typically put them on the element where they’re declared.

When to minify XML

Production systems exchange XML in minified form because:

  • Smaller payloads (whitespace is data too, in XML)
  • Some protocols are sensitive to whitespace in text nodes

For debugging, SOAP logging, or inspecting API responses, always format first.


Related posts

Related tool

XML Formatter

Format, validate, and beautify XML documents.

Written by Mian Ali Khalid. Part of the Data & Format pillar.