XML Formatter
Format, validate, and minify XML documents with syntax highlighting.
About this tool
XML Formatter is a free browser-based tool that instantly formats, validates, and minifies XML documents. It parses your raw XML string, checks it for syntax errors, and renders it with proper indentation and color-coded syntax highlighting β making even deeply nested XML structures easy to read and understand.
Paste your XML into the input field, then click Format to produce nicely indented output, or Minify to compress it into a single line for use in APIs, configuration files, and production environments. The validator highlights exactly where a syntax error occurs, such as unclosed tags or mismatched tag names, so you can fix it without guessing.
This tool runs entirely in your browser β no data is ever sent to a server. It is ideal for debugging web service responses, editing configuration files, testing API payloads, and preparing XML documents before sending them to a service or storing them in a database.
Frequently Asked Questions
Code Implementation
import xml.etree.ElementTree as ET
import xml.dom.minidom
# Format (pretty-print) XML
raw_xml = '<root><person><name>Alice</name><age>30</age></person></root>'
# Method 1: minidom (adds XML declaration)
dom = xml.dom.minidom.parseString(raw_xml)
pretty = dom.toprettyxml(indent=' ')
# Remove the extra XML declaration line if needed
lines = pretty.split('\n')
if lines[0].startswith('<?xml'):
pretty = '\n'.join(lines[1:])
print(pretty)
# Method 2: ElementTree (parse and re-serialize)
ET.indent # Available in Python 3.9+
root = ET.fromstring(raw_xml)
ET.indent(root, space=' ')
formatted = ET.tostring(root, encoding='unicode')
print(formatted)
# Minify XML
minified = ''.join(line.strip() for line in raw_xml.splitlines())
print(minified)Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.