Developer & Network Tools

XML, YAML and TOML Compared

Compare XML, YAML and TOML side by side, see the same data in each, and avoid the traps that break config files.

Key takeaways

  • XML uses nested tags and attributes, handles mixed text and markup well, and has mature validation tools.
  • YAML is compact and readable, but indentation and automatic type guessing cause subtle errors.
  • TOML uses explicit sections and types, which makes simple configuration hard to misread.
  • Quote any YAML value that should stay text, especially version numbers, codes with leading zeros and words like yes or no.
  • Use a real parser and a validator for all three, and never let untrusted files create arbitrary objects.

XML, YAML and TOML all store structured data as readable text, but each suits a different job. XML is a markup language built for documents and formal data exchange. YAML relies on indentation and is common in deployment and automation files. TOML is a deliberately small format for configuration that people edit by hand.

You rarely get a free choice: the tool you're using has already picked one. What you need is to read it correctly and recognize the mistakes each format invites.

The same settings, three ways

Here's a small server configuration in XML:

<server name="web1">
  <port>8080</port>
  <debug>false</debug>
  <hosts>
    <host>example.com</host>
    <host>www.example.com</host>
  </hosts>
</server>

In YAML:

server:
  name: web1
  port: 8080
  debug: false
  hosts:
    - example.com
    - www.example.com

And in TOML:

[server]
name = "web1"
port = 8080
debug = false
hosts = ["example.com", "www.example.com"]

The content is the same. What differs is how much is spelled out, and how types are decided.

Three text formats for structured data
XML Opening and closing tags Attributes and elements Everything is text without a schema Strong for documents and exchange YAML Indentation shows structure Types guessed from unquoted values Supports comments and reuse Common in automation files TOML Sections in square brackets Strings must be quoted Built-in date and time types Common in project settings XML Opening and closing tags Attributes and elements Everything is text without a schema Strong for documents and exchange YAML Indentation shows structure Types guessed from unquoted values Supports comments and reuse Common in automation files TOML Sections in square brackets Strings must be quoted Built-in date and time types Common in project settings

XML: tags, attributes and namespaces

XML, the Extensible Markup Language, is a W3C recommendation. A document has one root element containing others. Elements can carry attributes, and text can be mixed with child elements, which is why XML works well for publishing and office documents. Many familiar files are XML underneath, including SVG graphics, RSS and Atom feeds and the parts inside modern word processor files.

A document is well formed if it follows the syntax rules:

  • Exactly one root element.
  • Every tag closed, or written as self-closing.
  • Elements properly nested, not overlapping.
  • Names matched case for case.
  • Attribute values in quotes.
  • < and & escaped as &lt; and &amp; in text.

It is valid if it also matches a schema, written in XML Schema (XSD), RELAX NG or the older document type definition. Namespaces, declared with xmlns attributes, let one file combine vocabularies without name clashes. That power is also a common source of confusion: a query that ignores the namespace often finds nothing.

XML safety

XML's document type definitions can define entities that expand to other content. A parser configured to resolve external entities can be made to read local files or contact internal addresses, known as XML external entity injection. Entities that expand repeatedly can exhaust memory. When processing XML from outside, disable DTD processing and external entities, or use a library whose defaults already do.

YAML: indentation and guessed types

YAML expresses mappings with key: value, lists with a leading - , and nesting with indentation. Spaces are required; tabs aren't allowed for indentation. Comments start with #. It's a close relative of JSON, and YAML 1.2 was designed so that JSON documents are generally valid YAML.

Multi-line text has two main styles:

literal: |
  line one
  line two
folded: >
  these lines become
  one long line

Anchors (&name) and aliases (*name) let you reuse a block. They reduce repetition and can make a file harder to follow.

YAML's type traps

Unquoted values are interpreted, and the interpretation depends on which YAML version your tool implements.

You write What you may get
country: NO Boolean false in YAML 1.1 parsers
version: 1.10 The number 1.1
zip: 02134 A number, possibly read as octal
time: 12:30 A base 60 number in some 1.1 parsers
value: null An empty value, not the word
note: a #1 pick Text cut off at the comment

The rule of thumb is simple: if it should be a string, put quotes around it. "NO", "1.10" and "02134" stay exactly as typed.

Some YAML libraries can also construct arbitrary program objects from special tags. For anything you didn't write yourself, use the safe loading function, such as safe_load in the common Python library.

TOML: explicit and flat

TOML stands for Tom's Obvious, Minimal Language. A file is a set of key = value lines grouped into tables with headers in square brackets. Double square brackets create an item in an array of tables:

title = "Example"
released = 2026-12-05

[[backup]]
target = "disk"
keep_days = 30

[[backup]]
target = "remote"
keep_days = 90

Strings are always quoted, so the "is this a word or a Boolean" question doesn't arise. Dates and times are real types. Indentation is only cosmetic. The weakness shows with deep nesting, where long dotted table names such as [a.b.c.d] become tiring to read.

Comments, repetition and long files

How a format ages matters as much as how it starts. All three allow comments, which is a large advantage over plain JSON for settings that people maintain. XML comments are wrapped in <!-- and -->; YAML and TOML use # to the end of the line. Use them to record why a value was chosen, since the value itself already says what it is.

As files grow, repetition creeps in. YAML anchors and merge-style shortcuts can reduce it, though support for merging varies between parsers. XML projects often rely on includes or on tools that transform one file into another. TOML offers little for reuse on purpose, so repeated blocks stay visible and explicit.

Many systems layer files instead: a base file with defaults and a small override for each environment. Layering is clear while there are two or three levels. Document the order of precedence, and find out whether lists are replaced or combined, because tools disagree. When a setting seems to be ignored, a higher layer or an environment variable is the usual culprit. Many programs can print the final merged configuration, which is the fastest way to settle it.

Processing XML with queries

XML comes with mature companion languages. XPath selects parts of a document with path expressions, so //server[@name="web1"]/port finds the port of one named server in the example above. XSLT transforms XML into other XML, HTML or text. These are valuable when you work with large published vocabularies, industry exchange formats or document archives, and they explain why XML remains firmly in use.

YAML and TOML have no equally established standard query language. You load the file into your program's ordinary lists and dictionaries, or use a command line tool built for the purpose.

Which to use when

Picking a format when you have the choice
Does your tool or a standard require one? YES Use it, and follow that tool's own rules. 1 NO Is it a document with text and markup mixed? YES XML handles this naturally. 2 NO Is it mostly sent between programs? YES JSON is usually the simplest choice. 3 NO Will people hand edit fairly flat settings? YES TOML is clear and forgiving of layout. 4 NO Is it nested and hand edited, with lists? YES YAML is compact; quote strings carefully. 5 NO Default to the familiar Choose what your team can read and validate. Does your tool or a standard require one? 1 IF YES Use it, and follow that tool's own rules. NO Is it a document with text and markup mixed? 2 IF YES XML handles this naturally. NO Is it mostly sent between programs? 3 IF YES JSON is usually the simplest choice. NO Will people hand edit fairly flat settings? 4 IF YES TOML is clear and forgiving of layout. NO Is it nested and hand edited, with lists? 5 IF YES YAML is compact; quote strings carefully. NO Default to the familiar Choose what your team can read and validate.

For data exchanged between programs, see JSON explained. For tabular data, none of these is as compact as CSV, with its own hazards described in CSV pitfalls.

Shared habits that prevent trouble

Whatever the format:

  1. Save files as UTF-8, as covered in character encoding.
  2. Use an editor with syntax highlighting, and set it to insert spaces in YAML files.
  3. Validate in your build or deployment pipeline, so a typo fails before it reaches a live system.
  4. Check against a schema where one exists. Syntax checks won't catch a misspelled setting name that's silently ignored.
  5. Keep passwords and keys out of configuration files in version control; use environment variables or a secrets store.
  6. Review changes with a diff, and keep the file under version control as in Git basics.

Converting between them

Libraries can translate among these formats, but not always cleanly. XML attributes and mixed content have no exact equivalent in the others. TOML has no null value. YAML anchors disappear when expanded, and comments are usually lost by any parse-and-rewrite cycle.

If you convert a file, compare the loaded data and not only the text. Load the original and the result, and check that every key has the same value and the same type. That one step catches the version number that became a decimal and the postcode that lost its zero.

Secrets and sharing

Configuration files get pasted into chat messages and support tickets. Before you share one, replace tokens, passwords, private host names and personal details with obvious placeholders, and check the comments too, where old values often linger.

Reading an error message

Parser errors point to where the parser gave up, which can be after the real mistake. In XML, a "mismatched tag" report often means something earlier was never closed. In YAML, "mapping values are not allowed here" frequently means a colon followed by a space inside an unquoted value, or a line indented one space off. In TOML, a complaint about a redefined table usually means the same header appears twice.

When stuck, cut the file in half, check each part, and keep narrowing until the faulty line stands alone. Copying a working example from the tool's documentation and changing one setting at a time is slower to start and faster to finish.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in Developer & Network Tools →

Latest news on this topic