XML to YAML Converter

Keys in root—
Output lines—
Status—

How XML to YAML Converter is calculated: XML elements become YAML mappings, attributes become @name keys, and repeated elements become YAML lists; the reverse rebuilds the XML.

What is an XML to YAML converter?

An XML to YAML converter turns an XML document into YAML, and back again. XML is the verbose format of configs, feeds, and old APIs — angle brackets, closing tags, and attributes. YAML is the readable format — indentation and colons. The default XML here has a config root with 2 keys inside, and converts to 5 lines of YAML.

The need comes up whenever a system speaks XML and a human needs to read it. An old API returns XML, a config file is written in XML, or a feed needs editing by hand. YAML is far easier to scan: the same data in a third of the lines, with no closing tags to match.

This tool is pure JavaScript and runs entirely in your browser, with a small hand-written XML parser — no server, no library download for the XML side. The YAML side uses js-yaml, version 4.1.0, vendored on this site, with a built-in subset parser as a fallback.

How XML to YAML conversion works

Each XML element becomes a YAML mapping key. The root element, config, becomes the top-level key, and its children become keys underneath it: server and debug. Text inside an element becomes its value, so <debug>true</debug> becomes "debug: true" with the value typed as a boolean.

Attributes are kept with an @ prefix, so the server element's host and port attributes become "@host: localhost" and "@port: '8080'" inside the server mapping. The @ marks them as attributes rather than child elements, which is the convention this tool uses — and the reverse direction reads @ keys back into attributes.

Repeated elements become lists. Three <item> elements under one parent become a YAML list of three mappings, which is the only honest way to represent repetition in YAML. A single element stays a mapping, so the conversion is exact for the common shapes of XML.

Elements, attributes, and text, explained

An element is a tag with content: <debug>true</debug> is an element called debug with the text true. In YAML it is the key debug with the value true. An empty element such as <server /> becomes a mapping with no keys, which prints as an empty mapping.

An attribute is a name = value pair inside the opening tag: host="localhost" in <server host="localhost" />. Attributes describe the element rather than containing data, which is why they get the @ prefix in YAML — @host is clearly not a child element called host.

Text is the content between the tags. An element with both text and children keeps its text under the #text key, so nothing is lost. Text with special characters — <, >, and & — is escaped in XML as <, >, and &, and the converter unescapes them on the way in and escapes them again on the way out.

Attributes are the part of XML that has no direct equivalent in YAML, so the converter gives them a marker. An attribute becomes a key that starts with an at sign, so an element with an id attribute becomes a mapping with an @id key alongside its child elements. That convention is common enough that most tools that read the YAML will understand it, and it keeps the attribute separate from the child elements, which is important because an element can have both an attribute and a child with the same name. The at sign is the marker that tells the reader which is which.

A worked example: a config file

Take the default XML: a config root containing a self-closing server element with host and port attributes, and a debug element with the text true. That is 2 keys inside the root. Convert it and you get 5 lines of YAML: config at the top, server and debug underneath, with @host and @port inside server.

Reverse it: paste that YAML back, switch the direction to YAML to XML, and you get the XML again — the server element with its two attributes, and the debug element with true inside. The round trip is exact for elements, attributes, and text, which is the test of a good converter.

Try a list to see the third shape: an XML parent with three <item> children becomes a YAML list of three values. The converter counts repeated elements and builds the list automatically, which is the shape most configs use for repeated settings.

Who uses an XML to YAML converter, and why

Developers working with old APIs are the main users. An API that returns XML needs its responses read, debugged, and sometimes edited by hand, and YAML is far easier to read than a wall of angle brackets. Converting once makes the response scannable.

System administrators are the second group. Old config files — Maven's pom.xml, Android manifests, server configs — are XML, and an admin who needs to change one value can convert the file, edit the YAML, and convert it back, instead of hunting through nested tags.

Anyone migrating a feed or a data file is a third group. An RSS feed, a sitemap, or an export file in XML becomes YAML for editing, then converts back for the tool that reads it. A XML to JSON converter covers the same need for JSON.

Teams migrating old configs are a third group. A system that has stored its settings as XML for a decade will have hundreds of files, and the new system wants YAML, so each file must be converted and checked. The converter does the mechanical part in seconds; the slow part is reading the YAML and checking that the attributes landed under their at-sign keys and the repeated elements became lists. The conversion is worth doing because YAML is easier to edit by hand, and a config that is easy to edit is a config that gets kept up to date.

XML to YAML vs other converters

XML to YAML and XML to JSON do the same job in different formats. The XML to JSON converter on this site turns the same XML into a JSON object, which suits programs, while this tool turns it into YAML, which suits humans. The data is the same; the format is the choice.

YAML is the more readable of the two for a person editing a file. A YAML mapping can be read at a glance, while the same data as JSON is wrapped in braces and quotes. JSON is the better choice for a program to read. Pick the format your consumer wants.

A JSON formatter tidies JSON that is already JSON, and a JSON to YAML converter moves JSON to YAML. The tools together cover every direction a structured document might need to travel.

A converter is not a parser, and the distinction matters when the XML is broken. A parser reports the first error and stops, which is the right behaviour when the file must be fixed. A converter tries to make sense of what it is given, which is the right behaviour when the file is mostly right and the goal is to see the YAML shape. If the XML has a mismatched tag or an unclosed element, the converter will report it rather than guess, because a guess in a config file is worse than an error message.

Common XML to YAML mistakes

The first mistake is expecting comments to survive. XML comments are skipped on conversion, because YAML has its own comment style and there is no mapping between them. If a comment matters, copy it into the YAML by hand after converting.

Second: attributes and children with the same name. An element with both an attribute called name and a child called name produces a @name key and a name key, which is correct but confusing. Rename one of them before converting if the file is yours to change.

Third: mixed content. An element with text scattered between children — "some <b>bold</b> text" — keeps its text under #text, but the order of text and children is lost. Mixed content belongs in a document, not in data, so keep XML data clean: elements hold either text or children, not both.

Fourth: forgetting that XML is case-sensitive. <Server> and <server> are different elements, and they become different YAML keys. Check the case of your tags before converting, or the output will have two keys where you expected one.

A second mistake is expecting mixed content to survive. An element that holds both text and child elements — a paragraph with a bold word inside it — has no clean YAML form, because YAML mappings hold values and lists, not interleaved text and elements. The converter keeps the text under a #text key and the children as siblings, which preserves the data but loses the order the text and the children appeared in. If the order matters, the XML should stay XML, because the conversion is lossy for that shape.

How to use this XML to YAML converter

Paste your XML into the box — the default has 2 keys inside the root — and pick the direction. XML to YAML is the default. The converted output appears instantly in the box below, with the key count and line count above it.

To convert the other way, paste YAML with one root key and switch the direction to YAML to XML. To copy the result, use the Copy button next to the output box. If the input has an error, the output names the problem — fix it and paste again.

Everything runs in your browser. No sign-up, no account, no data sent anywhere — your XML never leaves your device. It works offline once the page has loaded, because the js-yaml library is vendored on this site rather than fetched from a CDN.

Paste the XML, press convert, and read the YAML before you copy it. The output is a mapping whose top-level key is the root element, and the children follow underneath in document order. Check that every repeated element became a list — a single child stays a mapping, but two children with the same tag become a list of two mappings, and a program reading the YAML will need to handle both shapes. When the shape looks right, copy the YAML into the config file it belongs to.

Frequently Asked Questions

With an @ prefix. The attribute host="localhost" on a server element becomes @host: localhost inside the server mapping. The reverse direction reads @ keys back into attributes, so the round trip is exact.

They become a YAML list. Three <item> elements under one parent become a list of three mappings, which is the standard way to represent repetition in YAML. A single element stays a mapping.

Yes — switch the direction to YAML to XML. The YAML must be a mapping with exactly one root key, which becomes the root element. @ keys become attributes and #text becomes the element text.

No. XML comments are skipped, because YAML has its own comment style and there is no mapping between them. If a comment matters, copy it into the YAML by hand after converting.

More free tools

More calculators and utilities from Sortrun — free, no sign-up.