XML (eXtensible Markup Language) predates JSON and, despite JSON winning the API world, it’s far from dead: SVG, RSS/Atom feeds, Office documents (.docx), Android layouts, Maven pom.xml and SOAP are all XML. Knowing how to read and tidy it is still a useful skill.
A formatter has to leave the meaning alone. We ran
<feed><item id="1"><title>Hello</title></item><item id="2"><title/></item></feed>through our XML formatter:<feed> <item id="1"> <title>Hello</title> </item> <item id="2"> <title/> </item> </feed>The self-closing
<title/>stays self-closing rather than being expanded to<title></title>, and theidattributes are untouched. That distinction matters: in XML those two forms are equivalent, but a consumer doing naive string matching may disagree, so a formatter that “tidies” them has changed your data. Indentation is the only thing that changed.
How XML is structured
XML describes data with nested elements wrapped in tags, which can carry attributes:
<book id="42" lang="en">
<title>The Hobbit</title>
<author>Tolkien</author>
</book>
Every opening tag needs a matching closing tag (or be self-closing, <br/>), tags must nest properly, and attribute values must be quoted. Get any of that wrong and the document won’t parse.
Structure is easiest to see when it is put back. This is one line of XML with two items, one of which has an empty title:
<feed><item id="1"><title>Hello</title></item><item id="2"><title/></item></feed>
and this is what our own XML formatter returned:
<feed>
<item id="1">
<title>Hello</title>
</item>
<item id="2">
<title/>
</item>
</feed>
Nothing was added or removed — only whitespace. Note that <title/> stayed self-closing rather than being expanded to <title></title>: the two are equivalent in XML, and a formatter that “helpfully” rewrote one into the other would be changing bytes that some signature or diff depends on.
Well-formed vs valid — an important distinction
These sound the same but aren’t:
- Well-formed means the XML follows the syntax rules above — correct tags, nesting and quoting. Any XML parser can check this.
- Valid means it also matches a specific schema (a DTD or XSD) that defines which elements and attributes are allowed. That needs the schema.
Most online tools — including ours — check that XML is well-formed. That catches the everyday errors (an unclosed tag, a stray &), which is what breaks parsing in practice.
XML vs JSON: when to use which
- JSON is lighter and the default for web APIs and configs that a program reads.
- XML shines where you need attributes, mixed content (text and elements together), namespaces, comments, or where an ecosystem already standardized on it (SVG, RSS, office formats).
They’re not competitors so much as tools for different jobs.
XML, JSON and HTML side by side
| Aspect | XML | JSON | HTML |
|---|---|---|---|
| Purpose | Store/transport data | Store/transport data | Display documents |
| Tags | Custom, any name | No tags (braces) | Fixed set (<p>, <div>) |
| Attributes | Yes | No (keys only) | Yes |
| Strictness | Must be well-formed | Strict | Lenient, browsers forgive errors |
| Comments | <!-- --> | None | <!-- --> |
XML and HTML look alike because both descend from SGML, but XML lets you invent your own tags and enforces strict syntax; HTML has a fixed vocabulary and browsers tolerate sloppy markup. XHTML was the attempt to make HTML obey XML’s rules.
A real-world example: RSS
An RSS feed is just XML with agreed-upon tag names:
<rss version="2.0">
<channel>
<title>My Blog</title>
<item>
<title>First post</title>
<link>https://example.com/first</link>
</item>
</channel>
</rss>
SVG images work the same way — an <svg> document is XML you can open in a text editor and edit by hand.
Common gotchas
- Escape the special characters. Five characters need entities in text:
<(<),>(>),&(&),"(") and'('). A raw&is the single most common cause of a broken feed. - XML is case-sensitive.
<Title>and<title>are different elements, and a closing tag must match exactly. - One root element only. A well-formed document has exactly one top-level element wrapping everything else.
Because SVG is XML, the same tidy-up applies when you edit vector images — see our guide on PNG to SVG.
That indentation is also where a common misconception lives. Whitespace between elements is still text as far as the XML data model is concerned, so a parser can report a text node where you expected only children. Reformatting <feed><item id="1"><title>Hello</title></item><item id="2"><title/></item></feed> into the indented version above does not change the document’s meaning, but it does change what firstChild returns — which is why XML libraries have a dedicated “ignorable whitespace” setting and JSON has no equivalent problem.
Format and validate it in your browser
The XML formatter beautifies messy XML into a readable indented structure, minifies it back to one line, and flags syntax errors — all locally, so even sensitive config never leaves your device.
Related tools
- Working with JSON instead? Use the JSON formatter.
- Converting between JSON and YAML? Try JSON ⇄ YAML.