Xerobit

XML to CSV Converter

Paste well-formed XML to convert it into CSV. The tool identifies the most frequent repeating element, flattens each instance into a row, and maps attributes (prefixed with @) and child text content into columns. Everything runs in your browser; nothing is uploaded to any server.

CSV Output

@id,title,author
1,XML Basics,Jane
2,CSV Guide,John

Record element: <book> (2 rows)

How to use the xml to csv converter

  1. Paste your well-formed XML into the input area.
  2. Choose a delimiter: comma, tab, or semicolon.
  3. The converter automatically finds the most frequent repeating element and flattens it into rows.
  4. Copy the resulting CSV from the output area.

How the XML to CSV conversion works

The converter uses a hand-written tokenizer to parse the XML without relying on the browser's DOMParser. It reads elements, attributes, text nodes, CDATA sections, comments, and processing instructions. Once parsed into a tree, it counts every element name across the document. The name that appears more than once and has the highest total count becomes the record element.

Each instance of that record element produces one CSV row. Attributes are mapped to columns prefixed with @, and direct child elements supply their text content as columns named after the child tag (a repeated child, or a child with nested elements, is joined with "; "). If a record has both attributes or children and its own text, that text goes in a #text column. For example, given <book id="1"><title>Guide</title></book> appearing twice, the headers become @id,title and each book fills one row.

Supported XML features and limits

The parser handles standard well-formed XML including nested elements, mixed attributes, character references like &#65; for 'A' or &#x41;, and the five built-in entities: &amp;, &lt;, &gt;, &quot;, and &apos;. CDATA sections are treated as raw text. Comments and processing instructions are skipped.

DOCTYPE declarations and external entities are not supported. If your XML relies on an external DTD to resolve entity references, the parser will report an error. This keeps the tool self-contained and safe, since resolving external entities can introduce security risks. If you need to inspect your structure first, try the xml viewer.

Error reporting with line and column

When the tokenizer encounters malformed input, such as an unclosed tag, a missing quote around an attribute value, or an invalid character reference, it stops and reports the exact line and column where the problem begins. This makes debugging straightforward compared to generic failure messages.

Line numbers start at 1 and increment on every newline character. Column numbers also start at 1 and reset after each newline. If you see an error like "Unexpected end of input at line 4, column 12", check that specific position in your source XML for a missing closing bracket or mismatched quote.

Common use cases for converting XML to CSV

Data exported from legacy systems often arrives as XML, but spreadsheet applications and data analysis pipelines prefer flat tables. Converting xml to csv bridges that gap. Typical scenarios include:

  • Exporting product catalogs from e-commerce platforms where each <product> element holds attributes like SKU and child elements for price and description.
  • Processing API responses that return lists of records wrapped in a root element.
  • Migrating configuration data or log entries into a tabular format for filtering and sorting.

If you later need to transform the same XML into other formats, tools like xml to yaml or xml to html handle those conversions. To compare two XML files structurally, use xml diff.

Reference: how fields map to columns

The following table shows how different XML constructs translate into CSV columns during the xml to csv conversion.

XML constructCSV column nameExample value
<item id="5">@id5
<name>Alice</name>nameAlice
<desc/> (empty)desc(empty string)
<val>&lt;10&gt;</val>val<10>
<raw><![CDATA[a,b]]></raw>rawa,b

Attributes always take the @ prefix to distinguish them from child elements that happen to share the same name. If a record lacks a particular child element or attribute, that cell is left empty in the CSV output.

Worked examples

ExampleInputResult
Book catalogXML input: <catalog><book id="1"><title>XML Basics</title><author>Jane</author></book><book id="2"><title>CSV Guide</title><author>John</author></book></catalog>; Delimiter: ,@id,title,author / 1,XML Basics,Jane / 2,CSV Guide,John
Employee recordsXML input: <company><dept name="Sales"><emp role="mgr">Alice</emp><emp role="rep">Bob</emp></dept></company>; Delimiter: ,@role,#text / mgr,Alice / rep,Bob
Tab-separated outputXML input: <items><item sku="A1"><price>9.99</price></item><item sku="B2"><price>4.50</price></item></items>; Delimiter: @sku price / A1 9.99 / B2 4.50

FAQ

How does the tool decide which element becomes a CSV row?

It counts every element name in the document. The name that appears more than once and has the highest count is chosen as the record element. Each occurrence of that element becomes one row in the output.

Are XML attributes included in the CSV?

Yes. Attributes on the record element are added as columns with an @ prefix. For example, an attribute named id becomes the column header @id.

Does this xml to csv converter upload my data?

No. The parsing and conversion happen entirely in your browser using a custom tokenizer. Nothing is sent to any server.

What happens if my XML contains commas inside values?

Values containing the chosen delimiter, double quotes, or newlines are wrapped in double quotes, and any internal double quotes are escaped by doubling them, following RFC 4180.

Can I convert XML with deeply nested structures?

Only direct children of the record element get a column. If such a child contains further elements, the text of all its descendants is joined into that one cell with "; ", and the nested element names are not kept. Flatten your XML first if you need one column per nested field.

Why do I get a parse error on valid-looking XML?

The parser requires strictly well-formed XML. Common issues include unescaped ampersands, missing closing tags, or DOCTYPE declarations, which are not supported. Check the reported line and column to locate the problem.