XML, TOML & Config Formats Cheat Sheet
Quick reference for configuration formats: XML structures, TOML tables/arrays, and when to choose each serialization.
XML, JSON, YAML, and TOML are the four main structured formats. XML is verbose and document-oriented (with attributes + namespaces); TOML is minimal and human-friendly for config files (INI evolved); JSON/YAML are the generic interchange pair.
XML Basics
XML is a tree of elements with attributes. Well-formedness requires exactly one root element, closed tags, and quoted attributes.
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="fiction">
<title>Book A</title>
<price currency="USD">8.95</price>
</book>
<book category="reference">
<title>Book C</title>
<price currency="USD">8.99</price>
</book>
</bookstore>
| Concept | Syntax | Example |
|---|---|---|
| Declaration | <?xml ...?> | <?xml version="1.0"?> |
| Element | <name>...</name> | <title>Book A</title> |
| Attribute | name="value" | category="fiction" |
| Self-closing | <empty /> | <linebreak /> |
| Comment | <!-- ... --> | <!-- generated --> |
| CDATA | <![CDATA[...]]> | Raw text without escaping |
| Namespace | xmlns:prefix | xmlns:xsd="http://www.w3.org/2001/XMLSchema" |
TOML Basics
TOML uses key-value pairs, tables ([name]), and arrays of tables ([[name]]). It is strict about types and does not allow tabs for indentation.
title = "My App"
version = "1.2.0"
debug = false
[server]
host = "0.0.0.0"
port = 8080
max_connections = 100
[database]
driver = "postgres"
url = "postgres://user:pass@localhost:5432/app"
[features.flags]
enable_ml = true
max_items = 50
[[servers]]
name = "east"
ip = "10.0.0.1"
[[servers]]
name = "west"
ip = "10.0.0.2"
| Construct | Syntax | Example |
|---|---|---|
| Key-value | key = value | port = 8080 |
| Table | [table] | [server] |
| Nested table | [a.b.c] | [database.pool] |
| Array of tables | [[name]] | [[servers]] (repeatable) |
| Inline table | { key = value } | point = { x = 1, y = 2 } |
| Array | [1, 2, 3] | ports = [80, 443] |
| Multi-line string | """...""" | Long text blocks |
Format Comparison
| Characteristic | JSON | YAML | TOML | XML |
|---|---|---|---|---|
| Comments | No | # | # | <!-- --> |
| Strings must be quoted | Yes | Sometimes | Yes | No |
| Type inference | No | Yes (dates, numbers) | Yes | No (all text) |
| Best for | APIs, data interchange | Config, CI files | Config files | Documents, SOAP, SVG |
| Namespaces/attributes | No | No | No | Yes |
| Human factor | Machine-friendly | Indentation-sensitive | Minimal, INI-like | Verbose |
Common Pitfalls
[!WARNING] YAML and TOML both auto-infer types — a value like
port: 8080becomes a number andversion: 1.0may be parsed as a float. Quote values when you need strings.
[!TIP] Use TOML for application config (it's the format of Cargo, pyproject, and many tools), JSON for API payloads, and XML only when the ecosystem requires it (SOAP, SVG, sitemaps).