How to Format XML Online
This free XML formatter runs entirely in your browser. Paste your XML into the left pane and click one of the action buttons to transform it instantly:
- Format — Adds proper indentation to make nested XML readable. Choose 2 spaces, 4 spaces, or tabs using the indent selector.
- Minify — Removes all unnecessary whitespace to produce compact XML, ideal for network transmission or storage.
- Validate — Checks that your XML is well-formed according to the XML specification. Errors are reported with descriptive messages.
- XML→JSON — Converts your XML document to a JSON representation using the Badgerfish convention.
- JSON→XML — Converts a Badgerfish-convention JSON object back to formatted XML.
After transformation, use Copy to copy the output to your clipboard, or Download to save it as a file.
Understanding XML Well-Formedness
Valid XML must follow strict rules that distinguish it from the more lenient HTML. The most common mistakes that break XML well-formedness:
- All tags must be closed. Use
<br/>or<br></br>— never just<br>. - Attribute values must be quoted. Write
width="100"— neverwidth=100. - Tag names are case-sensitive.
<Book>and<book>are different elements. - Exactly one root element. Multiple top-level tags make a document invalid XML (though they may be valid HTML fragments).
- Special characters must be escaped. Use
&for &,<for <,>for >,"for ", and'for '.
CDATA Sections
CDATA (Character Data) sections let you include raw text that would otherwise be interpreted as markup. Content inside <![CDATA[...]]> is preserved exactly as written, including literal < and > characters:
<description><![CDATA[Price: <$10 & free shipping]]></description>
This formatter preserves CDATA sections during both formatting and minification — the content between the brackets is never modified or reescaped.
XML vs HTML — Key Differences
HTML and XML look similar but have important differences that trip up developers. HTML is a specific application designed for web pages; XML is a generic, extensible data format:
- HTML tolerates unclosed tags and missing quotes; XML does not
- HTML attribute names and tag names are case-insensitive; XML is case-sensitive throughout
- HTML has predefined semantic tags like
<p>,<div>; XML tags are defined by you - HTML has "void elements" like
<br>that cannot have children; XML uses self-closing tags (<br/>) - Browsers render malformed HTML gracefully via error-recovery; XML parsers reject any well-formedness violation
XML to JSON Conversion
This tool uses the Badgerfish convention for XML↔JSON conversion, which provides a predictable round-trip for most XML structures:
- Element names become JSON keys
- Attributes are prefixed with
@— e.g.,@id,@class - Text content of a simple element becomes its value directly
- For mixed content (element with both text and child elements), text is stored under
#text— note that text fragments are concatenated, so the exact text–element interleaving cannot be recovered when converting back to XML - When the same tag name repeats as siblings, they are collected into a JSON array
Example — this XML:
<user id="42"> <name>Alice</name> <email>alice@example.com</email> </user>
Becomes this JSON:
{
"user": {
"@id": "42",
"name": "Alice",
"email": "alice@example.com"
}
}
Common XML Use Cases for Developers
XML powers many systems you interact with daily. Knowing when you're dealing with XML (and how to format it) is a practical skill:
- Build tools: Maven's
pom.xml, Gradle build files, Android'sAndroidManifest.xmland layout files - Web feeds: RSS 2.0 and Atom syndication feeds are XML
- Office documents: .docx, .xlsx, and .pptx are ZIP archives containing XML files
- Web services: SOAP-based APIs exchange XML envelopes — still common in banking, healthcare, and enterprise software
- Configuration: Spring Framework, Hibernate, and many Java frameworks use XML config files
- Vector graphics: SVG (Scalable Vector Graphics) is a subset of XML — SVG files can be formatted and validated here
- Localization: Android string resources (
strings.xml), XLIFF translation files, and many other i18n formats use XML