StringMash.com

XML to CSV

Flatten repeated XML elements into rows, with a column for every attribute and nested path.

307 characters
Updates as you type
One row per repeated element

Show the steps
  1. Rows: 2 <book> elements, found at catalog > book. It is the first level where an element repeats.
  2. Attributes are columns that start with @, or end with .@name when the attribute sits on a nested element.
  3. 2 rows and 5 columns.

Using the converter

Paste XML on the left. The converter looks for the element that repeats, makes each one a row, and gives every attribute and every element inside it a column. The working under the result says which element it picked and why.

Row element overrides the guess. Type book and every <book> becomes a row, wherever it sits in the file. Element names are case-sensitive, so Book and book are different elements.

Download saves the result as a .csv file. Separator switches to a semicolon or a tab for spreadsheets set up to expect one.

How the columns are named

An attribute on the row element is a column starting with @, so id="bk101" lands in @id. A nested element is named by its path from the row, joined with dots: a <city> inside an <address> is address.city. An attribute on a nested element adds .@name to its path, as in price.@currency.

Text inside the row element itself, with no element around it, goes in a column named after the row.

Columns appear in the order they are first seen, so the first row sets the shape and later rows add to it. A row missing an element gets an empty cell.

When an element repeats inside a row

A book with two authors has two <author> elements, and a CSV cell holds one value. By default each gets its own numbered column, author[1] and author[2], counted from 1 as XPath counts. The numbering applies to every row once any row has a second author, so author[1] means the same thing all the way down.

Joined in one cell keeps a single author column and puts both names in it, separated by a semicolon and a space. That is easier to read and harder to split again.

How it picks the row

It walks down from the root and stops at the first level where an element appears more than once under the same parent. If several names repeat at that level, the one with the most copies wins. An Atom feed allows several <link> elements and any number of <entry> elements directly under <feed>. With two links and twenty entries, the entries win and become rows, and the links inside each entry become columns.

If nothing repeats at all, the whole document is one row.

It refuses broken XML

XML has strict rules, and the converter checks them rather than guessing. An end tag has to match its start tag exactly, with no case folding, and there must be a single root element. A file that breaks a rule gets an error naming the first problem, because a guess at the structure would put values in the wrong columns without telling you.

Entities such as &amp; are decoded and CDATA sections are kept as text. Comments, processing instructions and the <?xml?> declaration are skipped. Fields holding the separator, a quote or a line break are quoted as RFC 4180 describes.

Related conversions

XML to JSON keeps the nesting that CSV flattens away. JSON to CSV does this same job for JSON, and reads a CSV back into JSON.

Questions

How do I convert XML to CSV?

Paste the XML above. Each repeated element becomes a row and each attribute or nested element a column. Use Download to save it as a .csv file.

What happens to XML attributes?

They become columns whose names start with @. An attribute on a nested element is named by the element's path, as in price.@currency.

Can I choose which element becomes a row?

Yes. Type its name in Row element. Left blank, the converter picks the first level where an element repeats.

Is my XML uploaded anywhere?

No. The conversion runs in your browser, and the XML never leaves your device.

Sources

Added . Last updated and reviewed . What's new