An .xml file is plain text that describes data using tags. You will meet XML in sitemaps, RSS feeds, configuration files, office documents, SVG images and countless data exchange systems. It looks like HTML, but its job is different.
What XML is
XML stands for Extensible Markup Language. It is a W3C recommendation, with XML 1.0 published in 1998. XML defines how to write structured documents, not a particular set of tags: you invent the tags that suit your data, or use a vocabulary someone else defined.
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book id="1">
<title>Dune</title>
<author>Frank Herbert</author>
</book>
</library>
The parts of an XML file
- XML declaration: the optional first line,
<?xml version="1.0" encoding="UTF-8"?>, stating the version and encoding. - Elements: a start tag, content and an end tag, such as
<title>Dune</title>. An empty element can be written<br/>. - Attributes: name-value pairs inside the start tag, such as
id="1". Values must be quoted. - Text content: the characters between tags.
- Comments:
<!-- like this -->. - Root element: there must be exactly one outermost element that contains everything else.
- Namespaces: prefixes such as
xmlns:svg="..."that distinguish tags from different vocabularies. - CDATA sections: blocks where special characters need no escaping; see XML special characters and CDATA.
XML versus HTML
They share a heritage, but differ in rules:
| XML | HTML | |
|---|---|---|
| Tag names | You define them | Fixed set |
| Closing tags | Always required | Some optional |
| Case | Case-sensitive | Mostly case-insensitive |
| Attribute quotes | Required | Often optional |
| Errors | Parser must stop (well-formedness) | Browsers recover |
XHTML was a version of HTML written to XML's rules. The strictness is deliberate: an XML parser must reject a malformed document instead of guessing.
Where you will find XML files
- Web: sitemaps, RSS and Atom feeds, SVG images
- Office documents: modern Word, Excel and PowerPoint files are ZIP archives of XML parts
- Configuration: build files, application settings, manifests
- Data exchange: many industry and government standards, and older web services
- Documents: publishing and documentation formats
Strengths and weaknesses
XML is self-describing, validates against schemas, supports namespaces and handles mixed text-and-markup content. It is verbose, which makes files larger and harder to read than JSON for plain data.
Well-formed and valid
Two different questions: is the document correctly written (well-formed), and does it follow a particular schema (valid)? See XML: well-formed vs valid.
How to open an XML file
Any text editor opens it. Browsers show many XML files as a tree. For structure and errors, use a tool that parses it: Docento's Text & Markdown Editor opens .xml files locally and reports well-formedness errors with the line number. More options are in how to open and read XML files.
Takeaway
XML is a strict, tag-based text format for structured data and documents, defined by your own element names. It underpins sitemaps, feeds, SVG and office files, and a parser will reject a file that breaks its rules.