XML Formatter
Format and pretty-print XML with syntax highlighting
XML Input
Formatted XML
What is XML Formatting?
XML formatting (also called XML pretty-printing or XML beautification) is the process of adding consistent indentation and line breaks to an XML document so that its hierarchical structure becomes visible. Raw XML from APIs, config generators, or serializers is often delivered as a single line with no whitespace between tags. An XML formatter parses that document into a tree, then re-serializes it with predictable spacing.
The XML 1.0 specification (W3C Recommendation, fifth edition) defines a strict grammar where every opening tag must have a matching closing tag or be self-closing, attributes must be quoted, and five characters (<, >, &, ", ') require entity escaping. A formatter must respect these rules while inserting only insignificant whitespace that does not alter the document's information set (infoset).
Formatted XML is easier to read during code review, easier to diff in version control, and easier to debug when a service returns unexpected data. The formatting operation itself is lossless: the logical content of the document stays identical, only the presentation changes. Tools like git diff and pull-request review platforms show only modified lines — consistently indented XML ensures those diffs reflect real changes, not reformatting noise.
<catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer's Guide</title><genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date></book><book id="bk102"><author>Ralls, Kim</author><title>Midnight Rain</title><genre>Fantasy</genre><price>5.95</price><publish_date>2000-12-16</publish_date></book></catalog>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
</book>
</catalog>Why Use an Online XML Formatter?
Formatting XML by hand is error-prone and slow, especially for documents with deep nesting or mixed namespaces. A browser-based formatter gives you a formatted result in under a second, regardless of file size.
XML Formatter Use Cases
XML Predefined Entity Reference
XML reserves five characters for its syntax. When these characters appear as literal text content or attribute values, they must be replaced with predefined entity references. A correct formatter preserves these entities during pretty-printing.
| Character | Role in XML | Entity |
|---|---|---|
| < | Start of tag | < |
| > | End of tag | > |
| & | Start of entity | & |
| " | Attribute delimiter | " |
| ' | Attribute delimiter | ' |
XML Indentation Styles Compared
There is no single standard for XML indentation. The choice depends on your team's conventions and the tools in your pipeline. Here are the three most common styles.
Code Examples: Format XML Programmatically
When you need to format XML inside a script or build process, every major language has a built-in or standard-library option. Below are working examples you can copy directly.
const raw = '<root><item>hello</item></root>'
const parser = new DOMParser()
const doc = parser.parseFromString(raw, 'application/xml')
const serializer = new XMLSerializer()
const xml = serializer.serializeToString(doc)
// Indent with XSLT (browser-native approach)
const xslt = new DOMParser().parseFromString(`
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
</xsl:template>
</xsl:stylesheet>`, 'application/xml')
const proc = new XSLTProcessor()
proc.importStylesheet(xslt)
const formatted = proc.transformToDocument(doc)
console.log(new XMLSerializer().serializeToString(formatted))
// → <root>\n <item>hello</item>\n</root>import xml.dom.minidom raw = '<root><item>hello</item><item>world</item></root>' dom = xml.dom.minidom.parseString(raw) print(dom.toprettyxml(indent=' ')) # → <?xml version="1.0" ?> # → <root> # → <item>hello</item> # → <item>world</item> # → </root> # With lxml (handles namespaces, XSD, large files) from lxml import etree tree = etree.fromstring(raw.encode()) print(etree.tostring(tree, pretty_print=True).decode())
package main
import (
"encoding/xml"
"fmt"
"strings"
)
func formatXML(raw string) (string, error) {
decoder := xml.NewDecoder(strings.NewReader(raw))
var out strings.Builder
encoder := xml.NewEncoder(&out)
encoder.Indent("", " ")
for {
tok, err := decoder.Token()
if err != nil {
break
}
encoder.EncodeToken(tok)
}
encoder.Flush()
return out.String(), nil
}
// formatXML("<a><b>1</b></a>") → "<a>\n <b>1</b>\n</a>"# Format XML file with xmllint (part of libxml2, pre-installed on macOS/Linux) xmllint --format input.xml > formatted.xml # Format from stdin echo '<a><b>1</b></a>' | xmllint --format - # → <?xml version="1.0"?> # → <a> # → <b>1</b> # → </a> # Validate and format at the same time xmllint --format --schema schema.xsd input.xml