XML to YAML
Convert XML to YAML format
XML Input
YAML Output
What is XML to YAML Conversion?
XML to YAML conversion transforms data from Extensible Markup Language (XML) into YAML Ain't Markup Language (YAML). XML uses angle-bracket tags with attributes to describe hierarchical data, while YAML represents the same structures through indentation and plain-text key-value pairs. Converting XML to YAML online is a common task when migrating configuration from XML-heavy systems like Java Spring, Maven, or .NET to platforms that prefer YAML, such as Kubernetes, Ansible, GitHub Actions, and Docker Compose.
The two formats differ in their data models. XML treats everything as text by default and relies on schema definitions (XSD, DTD) for type enforcement. YAML has native types: strings, integers, floats, booleans, nulls, sequences (arrays), and mappings (objects). During conversion, values like "true", "5432", and "3.14" can be interpreted as YAML native types instead of remaining strings. A careful converter quotes these values to preserve the original text representation from the XML source.
XML also supports constructs that have no YAML counterpart: attributes, namespaces, processing instructions, CDATA sections, and comments. The conversion must choose a convention for representing attributes (commonly underscore-prefixed keys like _attr) and decide whether to discard or flatten the rest. Understanding these trade-offs before converting helps you pick the right tool, configure it correctly, and verify that the YAML output matches the original XML intent.
<server>
<host>db.example.com</host>
<port>5432</port>
<credentials admin="true">
<username>deploy</username>
<password>s3cret</password>
</credentials>
<options>
<option>ssl=true</option>
<option>timeout=30</option>
</options>
</server>server:
host: db.example.com
port: "5432"
credentials:
_admin: "true"
username: deploy
password: s3cret
options:
option:
- ssl=true
- timeout=30Why Use an Online XML to YAML Converter?
Writing a conversion script by hand means dealing with attribute mapping, array detection for repeated elements, and YAML type-coercion edge cases. A browser-based converter handles all of that in one step, letting you inspect the YAML output and copy it directly into your config files.
XML to YAML Use Cases
XML to YAML Mapping Reference
XML and YAML have different data models. The table below shows how each XML construct maps to a YAML equivalent. Attributes are typically converted to underscore-prefixed keys, and repeated elements become YAML sequences. Some constructs like comments and processing instructions have no YAML representation and are discarded during conversion.
| XML Construct | XML Example | YAML Equivalent |
|---|---|---|
| Element | <name>text</name> | name: text |
| Nested elements | <a><b>1</b></a> | a:\n b: "1" |
| Attributes | <el attr="v"/> | el:\n _attr: v |
| Text + attributes | <el a="1">text</el> | el:\n _a: "1"\n _text: text |
| Repeated elements | <r><i>1</i><i>2</i></r> | r:\n i:\n - "1"\n - "2" |
| Empty element | <el/> | el: "" |
| CDATA | <![CDATA[raw]]> | Treated as plain text |
| Comments | <!-- note --> | Discarded (no YAML equivalent) |
| Namespaces | xmlns:ns="uri" | Prefix preserved or stripped |
| Boolean-like text | <flag>true</flag> | flag: "true" (quoted to stay string) |
XML vs YAML: Data Model Differences
Converting between XML and YAML is not a simple syntax swap. The formats have fundamental structural differences that affect how data is represented after conversion.
Code Examples
Below are working examples for converting XML to YAML in JavaScript, Python, Go, and the command line. Each example handles nested elements, attributes, and repeated sibling tags.
import { parseStringPromise } from 'xml2js'
import YAML from 'yaml'
const xml = `
<config>
<database host="localhost" port="5432">
<name>mydb</name>
</database>
<features>
<feature>auth</feature>
<feature>logging</feature>
</features>
</config>`
const obj = await parseStringPromise(xml, { explicitArray: false })
console.log(YAML.stringify(obj))
// → config:
// → database:
// → $:
// → host: localhost
// → port: "5432"
// → name: mydb
// → features:
// → feature:
// → - auth
// → - loggingimport xmltodict
import yaml
xml = """
<server>
<host>db.example.com</host>
<port>5432</port>
<replicas>
<replica>node-1</replica>
<replica>node-2</replica>
</replicas>
</server>
"""
# Step 1: XML → Python dict
data = xmltodict.parse(xml)
# Step 2: Python dict → YAML
print(yaml.dump(data, default_flow_style=False))
# → server:
# → host: db.example.com
# → port: '5432'
# → replicas:
# → replica:
# → - node-1
# → - node-2
# With the standard library only (no xmltodict)
import xml.etree.ElementTree as ET
def elem_to_dict(elem):
d = {}
if elem.attrib:
d.update({f"_{k}": v for k, v in elem.attrib.items()})
for child in elem:
val = elem_to_dict(child)
if child.tag in d:
if not isinstance(d[child.tag], list):
d[child.tag] = [d[child.tag]]
d[child.tag].append(val)
else:
d[child.tag] = val
if elem.text and elem.text.strip():
text = elem.text.strip()
return text if not d else {**d, "_text": text}
return d
root = ET.fromstring(xml)
print(yaml.dump({root.tag: elem_to_dict(root)}, default_flow_style=False))# xq is part of the yq package (pip install yq) # It parses XML via xq and outputs JSON, then pipe to yq for YAML echo '<config><host>localhost</host><port>8080</port></config>' | xq . | yq -y . # → config: # → host: localhost # → port: "8080" # Using xmlstarlet + yq (Go version: https://github.com/mikefarah/yq) xmlstarlet sel -t -c '/' input.xml | yq -p=xml -o=yaml # Reads XML from file and outputs YAML directly
package main
import (
"encoding/xml"
"fmt"
"strings"
"gopkg.in/yaml.v3"
)
type Server struct {
XMLName xml.Name `xml:"server"`
Host string `xml:"host" yaml:"host"`
Port int `xml:"port" yaml:"port"`
Options []string `xml:"options>option" yaml:"options"`
}
func main() {
data := `<server>
<host>db.example.com</host>
<port>5432</port>
<options><option>ssl=true</option><option>timeout=30</option></options>
</server>`
var srv Server
xml.NewDecoder(strings.NewReader(data)).Decode(&srv)
out, _ := yaml.Marshal(srv)
fmt.Println(string(out))
// → host: db.example.com
// → port: 5432
// → options:
// → - ssl=true
// → - timeout=30
}