Why Flatten XML to CSV?
XML feeds from partners, marketplaces, and government portals are rarely spreadsheet-friendly. Analysts need rows and columns. Converting XML to CSV flattens repeating records so Excel, Google Sheets, and BI tools can filter, sort, and chart the data.
The Flattening Strategy
Pick one repeating element as the "row". Everything else becomes columns.
- Repeating siblings (e.g. <item> under <items>) → one CSV row each.
- Child element text → columns named after the child.
- Attributes (id, sku) → columns, often prefixed or using the attribute name.
- Parent fields shared across children → duplicated on each child row, or kept in a separate table.
Example mapping
An order with two line items becomes two CSV rows that share order_id and customer columns.
order_id,customer_name,sku,qty
1001,Ada Lovelace,A1,2
1001,Ada Lovelace,B2,1Step-by-Step: In the Browser
Best for one-off partner files and quick reviews.
- Open the XML to CSV Converter.
- Paste or upload the .xml file.
- Confirm which repeating element became rows.
- Preview columns and sample values.
- Download CSV, then open in the Online CSV Editor or Excel.
After conversion
Check row counts against the number of repeating elements. Spot-check attributes that should have become columns. Fix headers if namespaces leaked into column names.
Step-by-Step: With Python
ElementTree plus csv.DictWriter is enough for many feeds.
import csv
import xml.etree.ElementTree as ET
root = ET.parse("orders.xml").getroot()
rows = []
for item in root.findall(".//item"):
order = item.find("..") # adjust to your structure
rows.append({
"sku": item.get("sku", ""),
"qty": item.get("qty", ""),
"name": (item.findtext("name") or "").strip(),
})
with open("items.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=["sku", "qty", "name"])
writer.writeheader()
writer.writerows(rows)Namespaces
If elements look like {http://example.com}item, register namespaces or strip them before findall. Ignoring namespaces is a common reason converters return zero rows.
Attributes, Text, and Mixed Content
Real XML mixes these freely. Decide a rule before you convert.
| Source | CSV column strategy |
|---|---|
| Attribute on record | Column named after the attribute |
| Child element text | Column named after the child |
| Same name in attr + child | Prefix: attr_id vs id |
| Mixed text + children | Prefer child elements; drop mixed text or store as note |
| Deep nesting | Dot notation (customer.email) or separate tables |
When One CSV Is Not Enough
Deep documents sometimes need multiple flat tables: orders.csv, line_items.csv, shipments.csv, linked by order_id. That is healthier than a mega-wide sheet with hundreds of sparse columns.
Real-World Examples
Common conversion jobs.
Marketplace catalog
Ops downloads an XML product feed, flattens variants to CSV, adjusts prices in Excel, then re-imports via CSV to XML or a merchant API.
RSS / Atom review
Editors convert a feed to CSV to filter titles and dates without reading raw markup.
Compliance extract
Analysts flatten a regulatory XML dump into CSV for pivot tables while keeping the original XML for audit.
Common Mistakes
Flattening bugs look like "missing data".
- Picking the wrong repeating element (parent instead of child).
- Dropping attributes entirely.
- Ignoring XML namespaces and getting empty results.
- Not quoting CSV fields that contain commas from XML text.
- Assuming one XML file always maps to one tidy sheet.
Best Practices
A short playbook for reliable flats.
- Identify the row element before converting.
- Preserve attributes as columns.
- Handle namespaces explicitly.
- Validate row counts against the source XML.
- Keep the original XML; regenerate CSV when the feed updates.
Why Use Convert CSV Online?
Convert CSV Online is free, browser-based, and requires no account for everyday conversions. The XML to CSV Converter flattens feeds for spreadsheet work; the Online CSV Editor lets you clean columns before Excel or a database import. Client-side workflows work on Windows, macOS, and Linux browsers.
Preview before you pipeline
Confirm the flattened shape once in the browser, then automate the same mapping in code if the feed is recurring.
Conclusion
XML to CSV succeeds when you choose the right repeating element, keep attributes, and verify row counts. Use a browser converter for one-offs and a small script when the feed arrives every night.
FAQ
How do I convert XML to CSV?
Identify the repeating record element, map children and attributes to columns, then use Convert CSV Online’s XML to CSV Converter—or write a small Python/Node script for recurring feeds.
Will nested XML convert cleanly?
Shallow nesting converts well. Deep trees may need dot-notation columns or multiple related CSV files linked by an ID.
What happens to XML attributes?
They should become CSV columns. If a converter drops them, pick a tool that maps attributes explicitly or handle them in code.
Why is my CSV empty after conversion?
Usually the wrong element was treated as a row, or XML namespaces blocked element matching. Check the document structure and namespaces.
Can I open the result in Excel?
Yes. Download UTF-8 CSV and open via Data → From Text/CSV, or convert further with CSV to Excel on Convert CSV Online.
Is online XML to CSV safe?
Convert CSV Online processes files in your browser for everyday conversions, so partner feeds stay on your machine.
References
Convert your CSV in the browser
Preview, clean, and convert CSV files free with Convert CSV Online—no installation and no account required for everyday conversions.