HomeToolsConversionXML to YAML Converter

XML to YAML Converter

Convert structured XML documents to clean and readable YAML format.

Conversion
Input Format
Converted Output

Read XML as an indented data tree

XML is explicit but noisy when the task is simply to inspect configuration-like values. YAML offers a more compact block notation, making parent-child relationships and repeated records easier to scan. This free XML to YAML converter parses an XML document in the browser, maps its root element into a YAML key, represents attributes with @-prefixed keys, represents mixed or attributed text with #text, and turns repeated sibling elements into sequences.

There are no mapping controls on this page. The policy is fixed, which makes quick conversion predictable: paste into Input Format, then read Converted Output. The example is preloaded and output updates whenever source text changes. A Copy button copies successful output; malformed XML is shown as an error in the output pane and disables copying.

Follow an order through conversion

Consider an XML order with an attribute and repeated lines:

<order id="A104">
  <customer>Rina</customer>
  <paid>false</paid>
  <line sku="P-1">Notebook</line>
  <line sku="P-2">Pen</line>
</order>

The resulting YAML follows this shape:

order:
  "@id": A104
  customer: Rina
  paid: false
  line:
    - "@sku": P-1
      "#text": Notebook
    - "@sku": P-2
      "#text": Pen

The outer order key preserves the document element. Because line occurs twice beneath the same parent, it becomes a YAML block sequence. Each line has an attribute, so the value is a mapping containing @sku; its text cannot be represented as the mapping itself and is stored under #text.

One subtle point: XML character data remains a JavaScript string during this conversion. The YAML serializer then decides when text needs quotes. The literal string false shown above may be emitted without quotes because its characters do not trigger the serializer’s quoting rules. A YAML 1.2 parser could resolve that plain scalar as a boolean, changing the type even though the converter retained a string internally. Review YAML type resolution whenever values resemble booleans, nulls, dates, or other schema-sensitive forms.

The fixed XML-to-object rules

Attributes

Every attribute is added before child content under a key formed by @ plus its XML name. id="A104" becomes @id: A104. Attribute values are not numerically coerced by this converter. Keys containing @ do not match its plain-key pattern, so they are JSON-quoted in the YAML text.

Leaf elements

An element with no child elements and no attributes becomes its trimmed text string. An entirely empty leaf becomes an empty string, serialized as "". If the leaf has attributes, it becomes a mapping; nonempty character data is added as #text.

Repeated siblings

Child elements are grouped by nodeName. One child with a given name becomes a direct mapping member. Two or more become an array, serialized as a YAML sequence. This means output shape depends on source cardinality: one <role> is a scalar or object, while two <role> elements produce a list.

Text beside child elements

Text and CDATA section values are accumulated and trimmed. If an element also has child elements, nonempty accumulated text is placed in #text. The converter does not preserve where each text segment appeared among child elements, so mixed content is flattened.

Keys and scalar quoting

Keys beginning with a letter or underscore and followed by letters, numbers, underscores, or hyphens are emitted plainly. Other keys are JSON-quoted. String values are quoted when they begin or end with whitespace, contain a set of YAML punctuation, or begin like a number. Empty text is emitted as ""; empty objects and arrays use {} and [].

Why the YAML is a projection, not a round trip

XML’s data model includes ordered nodes of different types. YAML mappings do not guarantee a natural representation for interleaved prose such as:

<warning>Press <key>Reset</key> after the light turns red.</warning>

The converter creates one key member and one #text member containing the combined surrounding text. It no longer records that <key> occurred between “Press” and “after.” XML comments and processing instructions are also ignored by the element traversal. CDATA content survives as characters, but the fact that CDATA syntax was used does not.

Namespaces present another compromise. Element and attribute names are based on nodeName, so a prefix can remain visible. Namespace declarations are attributes and therefore appear under @xmlns or a related qualified key. The converter does not resolve expanded names into {namespace URI, local name} pairs or check a vocabulary schema.

Because these transformations lose lexical and ordering information, converting the YAML back to XML would not reliably recreate the original document. Use output for consumption by a YAML-oriented system only after agreeing on this mapping.

An operations-friendly workflow

  1. Paste a well-formed document with exactly one root element into the left editor.
  2. If the right pane begins with Error:, use the browser parser message to locate malformed XML.
  3. Compare every @ key with the source attributes and every #text key with mixed or attributed content.
  4. Test optional repeated elements with zero, one, and multiple occurrences to understand shape changes.
  5. Inspect plain scalars such as true, false, null, dates, and numeric-looking identifiers under the YAML schema used downstream.
  6. Check mixed content manually; do not assume child/text ordering survives.
  7. Click Copy and paste into the destination’s YAML parser or schema validator.
  8. Keep the XML original as the source of truth until downstream behavior is verified.

The tool does not provide download, clear, indentation, attribute omission, or custom prefix controls. Editing the left pane is the way to change or remove input. Output always uses two-space nesting and ends with a newline.

Diagnosing conversion errors

Parsing uses DOMParser with the text/xml media type. A mismatched closing tag, malformed attribute, or other parser failure is detected through a parsererror element. The right pane displays the error prefixed with Error:. Browser engines format those details differently, so line and column wording is not guaranteed.

Passing that parser is a well-formedness gate, not document validation. The page does not process an XML Schema Definition, DTD validity constraints, RELAX NG grammar, or application-specific requirements. It will convert an element name that your target rejects and an attribute value outside a schema enumeration. Validate both the source vocabulary and resulting YAML model separately when contracts matter.

Where an online XML to YAML converter is useful

The fixed mapping works well for configuration-shaped XML: a Maven fragment, service settings, a simple feed item, a test fixture, or a small API response whose order is not semantically important. YAML’s indentation makes repeated resources and nested options easier to discuss in a pull request or support ticket.

It is a poor fit for document-centric XML, signatures, canonicalization, entity-dependent content, or namespace-heavy standards where exact node order matters. Production batch conversion also needs tested error handling and a documented scalar schema. Prototype here, then encode the accepted rules in a repeatable pipeline.

Conversion review card

  • Root element becomes the top-level YAML key.
  • Attributes use quoted @name keys.
  • Attributed text uses #text.
  • Repeated sibling names become sequences.
  • Whitespace around accumulated text is trimmed.
  • Comments and processing instructions are absent.
  • CDATA contributes text but loses its wrapper.
  • Mixed-content ordering is not retained.
  • No XML schema or YAML schema is validated.

FAQ for this mapping

Can I change @ or #text?

No. This converter uses fixed keys. The separate XML to JSON tool provides configurable attribute and text-key fields, but this YAML page does not.

Why did one repeated field become a scalar?

Arrays are created only when more than one sibling has the same name under a parent. A single occurrence is assigned directly.

Are XML numbers converted to YAML numbers?

The XML values remain strings during object creation. However, the emitted YAML may leave some strings unquoted, and a downstream YAML parser can resolve plain scalars according to its schema. Verify resulting types.

What happens to an empty element?

An empty leaf without attributes becomes an empty quoted string. An element that has attributes but no text becomes a mapping containing those attributes.

Does Copy include an error?

No. Copy is disabled when XML parsing sets an error or when there is no successful output.

Can it convert an XML fragment with several roots?

DOMParser expects an XML document. Wrap sibling fragments in one root element before conversion, then decide whether that wrapper belongs in your destination model.

Does the converter preserve comments?

No. Comments are not included in the object traversal and therefore do not appear in YAML.

Is this YAML guaranteed to match YAML 1.2 types?

The serializer produces YAML-like block text but does not run a standards-aware YAML parser afterward. Type-looking plain strings require special review with the parser that will consume them.

Learn More

Read our comprehensive guide to master this utility.

Read Guide →