YAML to XML
Convert YAML to XML format
YAML Input
XML Output
What Is YAML to XML Conversion?
YAML to XML conversion transforms data written in YAML (YAML Ain't Markup Language) into XML (Extensible Markup Language). YAML uses indentation and minimal punctuation to represent structured data, while XML uses opening and closing tags with an explicit hierarchy. Converting between the two is a routine task when systems that consume XML need to receive data originally authored in YAML.
YAML was designed for human readability. Configuration files for tools like Kubernetes, Ansible, Docker Compose, and GitHub Actions are written in YAML because it is compact and easy to edit by hand. XML, defined by the W3C XML 1.0 specification, has been the default data interchange format in enterprise systems, SOAP web services, and document-centric workflows for decades. When a YAML-authored configuration must feed into an XML-based pipeline, conversion is required.
The mapping between YAML and XML is not one-to-one. YAML supports data types like booleans, integers, floats, and null natively, while XML treats all content as text unless a schema (XSD or DTD) defines types externally. YAML sequences (lists) have no direct XML equivalent and must be represented as repeated elements. Knowing these differences helps you predict the output and avoid surprises.
Why Use This YAML to XML Converter?
Manually rewriting YAML as XML is tedious and error-prone, especially for deeply nested structures. This converter handles the structural translation instantly.
YAML to XML Use Cases
YAML to XML Type Mapping Reference
YAML data types do not map directly to XML. This table shows how each YAML construct translates to its XML equivalent. Understanding these rules helps you predict the output and troubleshoot unexpected results.
| YAML Type | YAML Example | XML Output |
|---|---|---|
| Mapping (key: value) | { name: Alice } | <name>Alice</name> |
| Sequence (- item) | - apple\n- banana | <item>apple</item><item>banana</item> |
| Nested mapping | user:\n name: Alice | <user><name>Alice</name></user> |
| Scalar (string) | greeting: hello world | <greeting>hello world</greeting> |
| Scalar (number) | count: 42 | <count>42</count> |
| Scalar (boolean) | active: true | <active>true</active> |
| Null | value: null | <value/> |
| Multiline string | bio: |\n Line one\n Line two | <bio>Line one\nLine two</bio> |
YAML vs XML: Format Differences
YAML and XML solve the same problem — structured data representation — with different trade-offs. Neither is universally better; the right choice depends on who or what consumes the data.
| Feature | YAML | XML |
|---|---|---|
| Syntax | Indentation-based | Tag-based (<tag>...</tag>) |
| Data types | Native (string, int, bool, null, float) | Text only (schema adds types) |
| Comments | # inline comments | <!-- block comments --> |
| Attributes | No native support | Yes (<tag attr="val">) |
| Namespaces | Not supported | Yes (xmlns:prefix) |
| File size | Smaller (no closing tags) | Larger (verbose tags) |
Code Examples
How to convert YAML to XML programmatically in different languages and environments:
import { parseDocument } from 'yaml'
import { js2xml } from 'xml-js'
const yamlStr = `
server:
host: localhost
port: 8080
ssl: true
`
const data = parseDocument(yamlStr).toJSON()
const xml = js2xml({ root: data }, { compact: true, spaces: 2 })
console.log(xml)
// → <root>
// → <server>
// → <host>localhost</host>
// → <port>8080</port>
// → <ssl>true</ssl>
// → </server>
// → </root>import yaml
import xml.etree.ElementTree as ET
yaml_str = """
database:
host: db.example.com
port: 5432
credentials:
user: admin
password: secret
"""
data = yaml.safe_load(yaml_str)
def dict_to_xml(tag, d):
elem = ET.Element(tag)
for key, val in d.items():
child = ET.SubElement(elem, key)
if isinstance(val, dict):
child.extend(dict_to_xml(key, val))
elem.remove(child)
elem.append(dict_to_xml(key, val))
else:
child.text = str(val)
return elem
root = dict_to_xml('root', data)
ET.indent(root, space=' ')
print(ET.tostring(root, encoding='unicode'))
# → <root>
# → <database>
# → <host>db.example.com</host>
# → <port>5432</port>
# → ...
# → </database>
# → </root>package main
import (
"encoding/xml"
"fmt"
"gopkg.in/yaml.v3"
)
type Server struct {
XMLName xml.Name `xml:"server"`
Host string `yaml:"host" xml:"host"`
Port int `yaml:"port" xml:"port"`
SSL bool `yaml:"ssl" xml:"ssl"`
}
func main() {
yamlData := []byte("host: localhost\nport: 8080\nssl: true")
var s Server
yaml.Unmarshal(yamlData, &s)
xmlBytes, _ := xml.MarshalIndent(s, "", " ")
fmt.Println(xml.Header + string(xmlBytes))
// → <?xml version="1.0" encoding="UTF-8"?>
// → <server>
// → <host>localhost</host>
// → <port>8080</port>
// → <ssl>true</ssl>
// → </server>
}# Using yq (YAML processor) with xq (XML wrapper around jq) # Install: pip install yq # Convert YAML file to XML yq -x . config.yaml # → <host>localhost</host><port>8080</port> # Pipe inline YAML through conversion echo "name: Alice" | yq -x . # → <name>Alice</name>