How to convert XML to CSV
Updated · 3 min read
XML exports from e-commerce platforms, ERPs, banks and content systems usually contain a list of records hidden inside a few wrapper elements. This converter finds the repeating element, turns each one into a row, and flattens its children and attributes into columns, ready for Excel or a database import.
- Paste the XML or drop an
.xmlfile. - The repeating element (for example
<product>or<item>) is detected and used for rows. - Check the column headers: nested elements appear as
parent.child, attributes as@name. - Download the CSV, or switch the target to Excel for an .xlsx file.
Example
XML input
<rss version="2.0">
<channel>
<title>Example News</title>
<item>
<title>Data privacy rules tighten</title>
<link>https://example.com/a</link>
<category>Tech</category>
</item>
<item>
<title>Markets close higher</title>
<link>https://example.com/b</link>
<category>Finance</category>
</item>
</channel>
</rss>CSV output
title,link,category
Data privacy rules tighten,https://example.com/a,Tech
Markets close higher,https://example.com/b,FinanceThe channel's own <title> is not part of the list, so the rows are taken from the repeated <item> elements, and a short notice tells you what was left out.
Your data stays on your device. The conversion runs in JavaScript inside this tab — files and pasted text are never uploaded to DataSwitch Pro.
How rows are detected
The converter steps through single wrapper elements (such as <rss><channel> or <export><products>) and looks at the children. If they all share the same name, each becomes a row. If the level is mixed, the most frequently repeated element is used for rows — <item> in an RSS feed, <url> in a sitemap, <entry> in Atom.
Attributes and nested elements
Attributes become columns prefixed with @, and nested elements are flattened with dots: <address><city> becomes address.city. When an element repeats inside a record (several <tag> elements, for example), the values are kept together in one cell as JSON text.
Typical uses
- Opening a Google Merchant or marketplace product feed in a spreadsheet.
- Exporting RSS or Atom feed items for analysis.
- Listing the URLs and dates of an XML sitemap.
- Turning an ERP or banking XML export into something accountants can filter.
Convert XML to CSV in code
For one-off files the tool above is quickest. To automate the same task in a script or pipeline, these snippets are a good starting point.
Python (pandas)
import pandas as pd # pip install pandas lxml
df = pd.read_xml("feed.xml", xpath="//item")
df.to_csv("feed.csv", index=False, encoding="utf-8-sig")
Node.js (fast-xml-parser)
// npm install fast-xml-parser
import { XMLParser } from "fast-xml-parser";
import { readFileSync, writeFileSync } from "node:fs";
const doc = new XMLParser().parse(readFileSync("feed.xml", "utf8"));
const items = [].concat(doc.rss.channel.item);
const esc = v => `"${String(v ?? "").replace(/"/g, '""')}"`;
const cols = Object.keys(items[0]);
writeFileSync("feed.csv", [cols, ...items.map(i => cols.map(c => i[c]))]
.map(r => r.map(esc).join(",")).join("\n"));
Frequently asked questions
Which element becomes a row?
The repeated element directly inside the root or inside its single wrapper element. If several kinds of elements repeat, the most frequent one is used and the others are reported in a notice.
Can I get JSON instead?
Yes — switch the target to JSON, or use the XML to JSON converter, which keeps the whole document structure.
What about deeply nested lists?
Lists inside a record are stored as JSON text in a single cell so no data is lost. For complex documents, converting to JSON first gives you full control.
Will Excel open the CSV correctly?
Yes. Downloads are UTF-8 with a byte-order mark; choose a semicolon under CSV output if your Excel uses European regional settings.
Is the XML uploaded anywhere?
No. It is parsed by your own browser.