XML to CSV Converter
Flatten an .xml file into a CSV table. The repeating element becomes the rows, its children and attributes become the columns, and nothing is uploaded.
Updated
How to convert XML to CSV
- Select Choose a .xml file, or drop the file onto the box. Try a sample file loads a three-product catalogue.
- Check the preview. Row 1 lists the column names built from element and attribute names. Each repeating record is one row.
- Select Download CSV (.csv).
From a tree to a table
XML describes a tree, and a CSV is a grid. The converter has to choose which level of the tree becomes the rows. It uses the element that is repeated most often, which is nearly always the record you care about.
The sample file looks like this:
<catalog>
<product id="1">
<name>Desk lamp</name>
<price currency="USD">24.00</price>
<stock>120</stock>
</product>
<product id="2">
<name>Monitor arm</name>
<price currency="USD">48.90</price>
<stock>35</stock>
</product>
</catalog>
product repeats, so each product is a row:
| @id | name | price.@currency | price | stock |
|---|---|---|---|---|
| 1 | Desk lamp | USD | 24.00 | 120 |
| 2 | Monitor arm | USD | 48.90 | 35 |
Naming rules for columns
| In the XML | Column name |
|---|---|
A child element with text, <name> |
name |
An attribute on the row element, id="1" |
@id |
An attribute on a child, <price currency="USD"> |
price.@currency |
A nested child, <address><city> |
address.city |
| The same child twice in one record | phone, then phone_2 |
| An element missing from some records | The column exists and the cell is empty |
Text is taken as written, with spaces at the start and end removed. Every value is exported as text, which is all a CSV can hold anyway.
What is left out
Elements outside the repeating record are not part of the table. A header block with a creation date, or a footer with totals, does not appear in the CSV. If the file holds two different kinds of repeating record, such as customers and invoices, only the more frequent kind is converted.
Convert XML to CSV with Excel
Excel can load XML and save the result as CSV.
With Power Query:
- Go to Data > Get Data > From File > From XML.
- Pick the file. In the Navigator, select the table that holds your records.
- Select Transform Data to expand nested columns, or Load to place the table on a sheet.
- Choose File > Save As > CSV UTF-8 (Comma delimited).
By opening the file:
- Choose File > Open and select the .xml file.
- In the Open XML dialog, choose As an XML table.
- Excel builds a table from the structure it infers. Save it as CSV.
Excel’s XML table import refuses some structures, such as lists nested inside other lists. The Power Query route copes with more.
Google Sheets cannot open an XML file from disk. Its IMPORTXML function reads from a web address only.
Things that go wrong
- “This XML could not be read. Check that every tag is closed.” XML is strict. One missing end tag or one bare
&stops the parser. A text editor with XML highlighting shows where the structure breaks. - The rows are the wrong element. In a file where line items outnumber orders, the line items become the rows and the order details are not repeated on each line. Power Query in Excel gives you more control over that kind of file.
- Only some columns are filled. Records in XML often leave optional elements out. Blank cells are expected.
- Columns named with a prefix and colon. Namespaced elements keep their prefix, so
<g:price>becomes a column calledg:price. - “No repeating records were found.” The file holds only bare text elements with no nesting and no attributes, so nothing can serve as a record.
- An HTML page saved as .xml. HTML is rarely valid XML. Copy the table from the page and paste it into a spreadsheet instead.
How the CSV is written
The file is UTF-8 with a byte order mark, comma separated, with quotes around any value that contains a comma, a quote or a line break. Excel opens it with accented characters intact. When you open it in Excel, number-like text such as 00501 is converted to a number. To prevent that, import it with Data > Get Data > From File > From Text/CSV and set the column to Text, or convert the XML with XML to Excel, which stores every value as text.
Limits
- Files up to 50 MB.
- One repeating element per conversion. Other parts of the document are ignored.
- Repeating children inside a record become numbered columns in the same row. They are never split into extra rows.
- The preview shows the first 200 rows and 40 columns. The CSV has everything.
Questions
How do I convert XML to CSV?
Choose the .xml file above. The converter finds the element that repeats most often, turns each one into a row, and shows the table. Select Download CSV (.csv) to save it.
How does the converter decide what a row is?
It counts the elements that have child elements or attributes and picks the one that occurs most often under the same parent. In a product feed that is the product element, and in an order export it is the order or line element.
What happens to XML attributes?
Each attribute becomes a column whose name starts with @. An id attribute on the row element becomes @id, and a currency attribute on a price element becomes price.@currency.
Can it convert nested XML?
Yes, within one row. Elements nested inside a record are flattened into columns with dotted names such as address.city. A child that repeats inside one record gets numbered columns, for example item and item_2.
Why does the tool say the XML could not be read?
The file is not well-formed. Look for a tag that is never closed, an ampersand that should be written as &, or text before the first tag.
Is the XML file uploaded?
No. The file is parsed by your browser's own XML parser, and the CSV is created on your device.