What XML is, how it differs from HTML and JSON, and where it still runs the world

XML was going to be the universal format for everything, and for a few years around 2000 it nearly was. Today most new APIs use JSON, but XML quietly underlies Office documents, SVG, RSS feeds, sitemaps, Android layouts and most enterprise integration. Reading it is a useful skill; the XML formatter makes it readable and reports where it's broken.

What XML is

Extensible Markup Language (W3C, 1998) is a text format for tree-structured data: elements with a start tag, content and an end tag, nested inside one another, with attributes on the tags. It defines no tags of its own โ€” "extensible" means each application defines its vocabulary โ€” and its strictness was the point: unlike HTML of the era, which browsers forgave, an XML parser rejects any document that breaks the rules, so software can rely on the structure.

Well-formed: the rules

<?xml version="1.0" encoding="UTF-8"?>
<order id="1042">
  <customer>Ada Lovelace</customer>
  <item sku="A-7" qty="2">Analytical engine part</item>
  <note/>
</order>

A document obeying these is well-formed; one that also matches a schema is valid. Most "XML errors" are a missing end tag or an unescaped ampersand in a URL.

Elements, attributes, namespaces, schemas

Data can go in child elements or in attributes; the convention is attributes for metadata (ids, types) and elements for content, but vocabularies differ and it's the first thing to check when reading a new one. Namespaces (xmlns="โ€ฆ") let one document mix vocabularies โ€” an SVG inside an HTML page, Office's dozen sub-formats โ€” by prefixing element names with a URI-identified vocabulary. Schemas (DTD, XML Schema, RELAX NG) describe what a valid document may contain, and validators enforce them, which is why XML persists in regulated exchange: a bank file that doesn't match the schema is rejected before a human sees it.

Why JSON won for APIs

XML's verbosity (every value wrapped in an opening and closing tag), its impedance mismatch with program data (is this an element, an attribute, text?), and the heavy toolchain around it (SOAP, WSDL, XSLT) made web developers reach for JSON, which maps directly onto the objects and arrays in JavaScript and every other language (What JSON is, and where it trips people up). By 2010 JSON was the default for new web APIs; by 2020 it was rare to see XML in one. The remaining strengths โ€” schemas, namespaces, mixed content (text with markup inside it), comments, and mature validation โ€” are precisely the things document formats need and APIs mostly don't.

Where XML still lives

Converting is straightforward for simple documents โ€” XML to JSON and JSON to XML โ€” and lossy for the features JSON lacks (attributes versus elements, mixed content, namespaces), which the converters flatten by convention.

Sources and further reading

The claims in this guide rest on these references, which were checked when the guide was last updated. Spotted an error? The contact page says how to report it.

  1. XML โ€” Wikipedia
  2. Extensible Markup Language (XML) 1.0 โ€” W3C

Try the tool

Frequently asked questions

What is the difference between XML and HTML?

HTML is a fixed vocabulary for web pages that browsers forgive; XML is a strict syntax for defining your own vocabularies, and parsers reject any error. XHTML was HTML written under XML's rules.

Why does my XML fail to parse?

Usually an unescaped & in a URL or text, a missing end tag, or tags closed in the wrong order. The formatter reports the line.

Is XML obsolete?

For new web APIs, mostly replaced by JSON. It remains the standard for Office files, SVG, feeds, sitemaps and regulated data exchange with schemas.

Elements or attributes?

Convention: attributes for identifiers and metadata, elements for content and anything that might repeat or need structure. Follow the vocabulary you're working with.