How to convert XML to CSV
- Drop an
.xmlfile into the box above or paste the markup in. Nothing is uploaded either way. - Press Convert to CSV. The record element is chosen for you and every other candidate is listed underneath with its numbers.
- If the top pick is not the table you wanted, choose another from the list — the CSV is rebuilt immediately.
- For Excel, pick the semicolon delimiter if your locale uses it and leave the byte order mark on.
The hard part is deciding what a row is
Commas are not the difficulty in this conversion. XML is a tree and a CSV is a rectangle, so something has to decide which level of the tree is a row — and most converters make that your problem. The best-equipped one we found offers a text box reading “use this path to identify starting node” and the advice that your XML “should be record oriented in order to get good results”. It never says what the options were.
This page works them out. Every element path is measured twice — how many times it occurs, and how many distinct fields it would contribute — and the candidates are ranked by the two multiplied together, because a table is both tall and wide. You see the list with the numbers and can override the pick.
Why counting occurrences alone is not enough
This sounds like a detail until you try it on an ordinary catalogue: three books, with
four authors between them. There are more author elements than
book elements, so a ranking that looks only at how often something occurs
picks the authors and hands you a one-column list of names instead of a table of books.
| Candidate | Records | Fields | Ranked by occurrence | Ranked by rows × fields |
|---|---|---|---|---|
catalog/book | 3 | 5 | Second | First |
catalog/book/author | 4 | 1 | First | Second |
Elements that carry nothing but their own text are weighted down for the same reason: they are the cells of a table, not its rows.
A column has to mean the same thing on every row
Records repeat fields — a book with two authors, a product with three tags. The
obvious way to handle that is to number them as you go, so the two-author book fills
author.1 and author.2. Do it record by record and a book with
one author fills a plain author column instead, and now the first author
of a book is in one column on some rows and a different column on others. Sort the
spreadsheet and the damage is permanent.
So the decision is made per column across the whole file: if any record repeats a
field, that field is numbered on every record, and a book with one author
fills author.1 with author.2 left empty. You can also join
repeats into a single cell if you would rather have one column.
Columns come from every record, not the first one
The quick way to build a header is to read the first record and use its fields. It works until the schema has grown — the field added last year exists only on recent entries, is missing from record one, and vanishes from the output without a word. Here every record is read before the header is written, and the report says how many columns appeared after the first record and how many records were missing each one.
That last number is worth reading. An empty cell in a CSV cannot distinguish “this field was blank” from “this field was not there”, and the report is the only place the difference survives.
What happens to nesting and attributes
| In the XML | Column |
|---|---|
| A child element of the record | title |
| An attribute on the record | @isbn |
| A child element inside a child | price.amount |
| An attribute on a nested element | price.@currency |
| A child element that occurs twice | author.1, author.2 |
The @ on attributes is not decoration: an element may legally carry an
attribute and a child element with the same name, and without the prefix one of the
two would overwrite the other.
The CSV itself
Values containing the delimiter, a double quote, a line break or leading and trailing spaces are quoted as RFC 4180 requires, so a description with a line break in it stays one cell instead of splitting the row in half. Line endings are CRLF, which is what Excel expects, and the optional byte order mark is what tells Excel on Windows to read the file as UTF-8 rather than turning accented characters into pairs of symbols.
Next steps
If the XML is too irregular to be a table, XML to JSON keeps the nesting — and keeps repeating elements as arrays so the shape does not change between files. JSON to XML goes back the other way. Once you have a CSV, split CSV breaks a large one into pieces without cutting through a quoted field, and CSV to JSON converts it onward. All of them are on the data tools page.