Convert YAML to XML

Converting YAML to XML turns each mapping key into an element and each list into that element repeated, which is how XML has always expressed a sequence. Keys are also the control surface: name one with a leading @ and it becomes an attribute rather than a child, which is the difference between XML a schema accepts and XML it rejects.

  • Where it runs In your browser. The file is never uploaded.
  • Lossless Nothing is discarded. The XML holds exactly what the YAML held.
  • File size limit Up to 100 MB per file, free, without an account.

Up to 100 files at once. Mixed formats are fine.

How a YAML mapping becomes a tree of tags

Each key becomes an element and its value becomes the content of that element. A nested mapping becomes a nested element, indented two spaces per level, and a scalar becomes text between an opening and a closing tag. Nothing about the traversal is clever, which is the property you want when the output has to match a specification somebody else wrote.

Values are written as text, because that is all an XML element can hold. A YAML number written 1.50 comes out as 1.5, since the parser resolved it to a number before the writer saw it, and a boolean comes out as the word true or false. If the receiving schema is strict about lexical form — a decimal that must keep its trailing zero, a date that must carry a timezone — the value has to be quoted as a string in the YAML so it survives verbatim.

Which YAML key becomes the root element

XML requires exactly one outermost element and a YAML document can have several top-level keys. The rule is: one top-level key becomes the root, whatever it holds, unless what it holds is a list. So a document whose only key is order produces an order root; so does one whose only key is title with a scalar under it, giving the single-line document <title>hello</title>. A document with three top-level keys produces a generic root containing three children.

A single top-level key holding a list is the case that surprises people. It also gets the generic wrapper, because using the key as the root would repeat that tag once per item and leave a document with several outermost elements, which is not XML at all. If the root element name matters — and in an integration it always does — give the YAML one top-level mapping key named exactly what the schema expects, and put everything under it.

Lists become repeated tags rather than a wrapper

A YAML sequence under a key emits that key once per item. Three build commands under a script key become three script elements in order, siblings of each other. This is the older and more common XML idiom, and it is what an XSD written with maxOccurs unbounded expects.

It is not the only idiom. Plenty of schemas want a plural wrapper containing singular children — a dependencies element holding several dependency elements — and this writer will not produce that from a plain list. You get it by writing the YAML that way: a key named dependencies whose value is a mapping with one key named dependency holding the list. The nesting in the source is what produces the nesting in the output, so shape the YAML to the schema rather than expecting the converter to guess.

Writing attributes from a YAML file

Elements-only XML is fine for data and wrong for most real schemas, which put identifiers, types and language codes in attributes. Two key names give you control. A key whose name starts with @ becomes an attribute on the enclosing element with the @ stripped, and a key named #text becomes the element own text content.

Written out, a YAML mapping named order containing "@id" set to 42, "@status" set to open, and a customer key produces an order element carrying id and status attributes with a customer child inside it. Combining "@lang" and "#text" in one mapping produces a single tag with an attribute and text and no children. Both keys need quoting in the YAML, since @ at the start of a plain scalar is reserved and # would start a comment.

Key names XML will not accept, and what happens to them

XML element names allow letters, digits, underscores, hyphens and dots, and may not begin with a digit. YAML keys allow essentially anything. Every character the writer cannot use becomes an underscore, and a name starting with a digit is prefixed with one, so "my key" becomes my_key and "2nd" becomes _2nd.

The substitution loses information and can collide: two keys differing only in which forbidden character they contain end up as the same element name. Hyphens and dots pass through untouched, so a key like build-job or tool.ruff is written as you typed it. If the target schema names the elements, rename the YAML keys to match before converting rather than repairing the XML afterwards.

What the XML output does not include

There is no XML declaration, so the file starts with the first tag. There is no DOCTYPE, no namespace declaration and no schema reference. The output is UTF-8 and well-formed, and nothing in it says which vocabulary it belongs to.

For an integration that is usually a gap to close by hand, and it is a small one: a declaration line, an xmlns attribute on the root, and a schema location. The xmlns attribute is easy to add at the source, since it is just another key beginning with @ in the top-level mapping of the YAML — which means the namespace survives every future conversion instead of being re-added each time.

Empty values, special characters and multi-line text

A YAML key with no value becomes an empty element pair. XML cannot distinguish that from an element containing an empty string, so if the schema treats absent and empty differently, leave the key out of the YAML rather than writing it with nothing after it.

Ampersands, angle brackets and double quotes in a value are escaped, so text containing a URL with query parameters or a fragment of code comes through safely rather than breaking the document. A multi-line block scalar is written with its newlines intact inside the element, which is valid XML — but whether the receiving parser preserves that whitespace depends on the schema, so anything whitespace-sensitive belongs in a CDATA section that you add yourself.

Validating the result, which is usually why you are here

XML has been a W3C recommendation since 1998, and the reason to emit it rather than JSON is almost always the ecosystem rather than the syntax: XSD to reject a malformed document before it reaches an application, XPath to pull one value out of a large one, XSLT to reshape it, and XML digital signatures where a counterpart needs to prove a document was not altered.

None of it comes for free with the conversion. Run the output against the schema immediately, on the first file, before wiring the process up — an element that should have been an attribute is a two-character change in the YAML and a long afternoon if you find it after building a pipeline around the wrong shape.

The YAML files that will not convert at all

Two source problems stop the conversion rather than degrading it. A file containing --- separators holds several YAML documents, and an XML file holds one, so the parser refuses instead of choosing. Split it and convert each document. A duplicate key in the same mapping is also rejected, with the line number, which is stricter than some YAML tooling and correct — a duplicated key means one of the two settings has been ignored since the day it was added.

Comments do not stop anything and do not survive either. Both formats support them and none of yours cross over, so the XML you deliver carries the data and none of the reasoning. Keep the YAML as the source of truth, generate the XML as an artefact, and never edit the XML — that is the working arrangement this conversion is built for.

How to convert YAML to XML

  1. Drop your YAML file onto this page, or click to choose one.
  2. It is converted to XML in your browser.
  3. Download the XML, then validate it against your schema.

YAML against XML: indentation traded for tags

YAML compared with XML
YAMLXML
Full nameYAML Ain't Markup LanguageExtensible Markup Language
File extension.yaml, .yml.xml
Media typeapplication/yamlapplication/xml
First published20011998
Published byW3C
SpecificationYAML 1.2XML 1.0
LicensingOpen standardOpen standard
Standing todayCurrentCurrent
Opens in a browserNo browserEvery browser
Considered insteadJSON, TOMLJSON

What survives

Nothing is discarded. YAML and XML both store their content losslessly, so the conversion is a change of packaging rather than a change of quality, and it can be repeated without accumulating damage.

Comments carry across. Both YAML and XML have a comment syntax, so notes left for whoever maintains the file next are not silently thrown away.

Opening the result

XML opens in every current browser. YAML has narrower browser support than that. If the file is going onto a web page or into a form, that is usually the whole reason for the conversion.

Visual Studio Code reads both YAML and XML, so there is a way to check the result against the original without a second tool.

What each format is for

YAML was published in 2001. The specification is YAML 1.2, and it is worth reading if the file has to outlive the tool that wrote it.

XML comes from W3C and dates from 1998, specified as XML 1.0. Visual Studio Code and oXygen XML Editor all read it.

YAML to XML: questions from an integration project

Are my YAML files uploaded anywhere?

No. This conversion runs entirely inside your browser, so the file never leaves your device. You can confirm it yourself: open the network tab of your browser's developer tools and convert something. You will see the page load, plus the analytics and advertising the site is paid for with — and nothing carrying your file.

How is a list represented in the XML?

As a repeated tag, which is how XML has always held a sequence. A list of two values under a key named stages becomes two stages elements side by side. There is no wrapper element around the set.

Can I get attributes instead of child elements?

Yes. A key whose name begins with @ is written as an attribute of the enclosing element, so a YAML key "@id" with value 42 produces id="42" on the tag. A key named "#text" becomes the element own text content, which lets you mix an attribute and a value on one tag.

What becomes the root element?

If the document has exactly one top-level key and that key does not hold a list, it becomes the root. Otherwise the output is wrapped in a root element, because XML permits only one outermost tag and a list of siblings would produce several.

Is there an XML declaration at the top?

No. The output begins with the first element. It is UTF-8, so most parsers are correct without a declaration, but add one by hand if your validator or your recipient requires it.

What happens to keys that are not valid element names?

Characters XML does not allow in a name are replaced with an underscore, and a name starting with a digit gets an underscore in front of it. A key written "my key" becomes my_key and a key written "2nd" becomes _2nd.

Does the file leave my machine?

No. The YAML parser and the XML writer both run in this page as JavaScript, so nothing is uploaded, and the free tier accepts files up to 100 MB.

More about these formats