I would recommend pandasread_xml() and to_csv() function, 3-liner:

Compare the documentation: to_csv, read_xml

import pandas as pd

df = pd.read_xml('employee.xml')
df.to_csv('out.csv', index=False)

Output -> (CSV-file):

id,name,age,salary,division
303,varma,20,120000,3
304,Cyril,20,900000,3
305,Yojith,20,900000,3
Answer from Hermann12 on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-xml-to-csv-in-python
Convert XML to CSV in Python - GeeksforGeeks
July 23, 2025 - Export the DataFrame to a CSV file. To download the XML data used in the examples, click here. Python Program to Convert XML to CSV.
🌐
GitHub
github.com › waheed0332 › xml2csv
GitHub - waheed0332/xml2csv: Python scripts for processing XML documents and converting to CSV. Also works on nested xml files. · GitHub
This script utilize power of multiprocessing to convert huge data in less time. Install required libraries using following command before running script. pip install -r requirements.txt · python xml2csv.py -f ./xml-samples/1.xml -csv out.csv
Starred by 23 users
Forked by 7 users
Languages   Python
Discussions

Convert xml to excel/csv
Please help me in converting XML file into excel/csv. Thank you in advance. More on discuss.python.org
🌐 discuss.python.org
0
0
October 15, 2022
pandas - XML to CSV Python - Stack Overflow
Parsing and conversion to CSV of above mentioned XML file will be done by the following python code. More on stackoverflow.com
🌐 stackoverflow.com
March 22, 2025
how to convert xml file to csv using python script - Stack Overflow
I have an xml file which have a special structure , I need to convert it to csv file using a script python This is a part of my xml File : More on stackoverflow.com
🌐 stackoverflow.com
python - Convert XML to CSV file - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
March 1, 2024
People also ask

How to Convert XML to CSV via Python?
You can convert XML to CSV using ElementTree module of the in-built xml module in Python. It can parse an XML document as a parameter and save the file in the form of tree, which has a getroot() method that returns the root element of the tree. Here is the code to convert XML to CSV via Python.
🌐
pdf.wondershare.com
pdf.wondershare.com › home › pdf converter › how to convert xml to csv in 5 easy ways
How to Convert XML to CSV in 5 Easy Ways
What is CSV Format?
Released in 2005 the CSV or the Comma Separated Values are the ones which can be used to store tabular data with ease and satisfaction. The RFC 4180 standard is used to standardize the values of the CSV format. There are many character sets that can be used to denote the CSV formatting. It includes the ASCII, Unicode, EBCDIC, and Shift JIS. The main part of the CSV file is the record handling which is highly regarded by users all over the world. One record per file can be stored with ease. The sequence of fields also remains the same in CSV format.
🌐
pdf.wondershare.com
pdf.wondershare.com › home › pdf converter › how to convert xml to csv in 5 easy ways
How to Convert XML to CSV in 5 Easy Ways
How do I convert an XML file to Excel?
If you need a smart and easy solution to convert XML to Excel format, try Wondershare PDFelement. It's an AI-powered software that aims to enhance your workflow with easy processing. To know how easily it works, look into the steps next: Step 1: Launch PDFelement and click the "+" icon from the top-left side. Then, choose "From File" and open your XML file. Step 2: Now, tap the "File" from the top toolbar and select the "Export To" option. From the extended menu, click the "Excel" option and eventually the "Save" button to get the file converted into Excel.
🌐
pdf.wondershare.com
pdf.wondershare.com › home › pdf converter › how to convert xml to csv in 5 easy ways
How to Convert XML to CSV in 5 Easy Ways
Top answer
1 of 2
7

I would recommend pandasread_xml() and to_csv() function, 3-liner:

Compare the documentation: to_csv, read_xml

import pandas as pd

df = pd.read_xml('employee.xml')
df.to_csv('out.csv', index=False)

Output -> (CSV-file):

id,name,age,salary,division
303,varma,20,120000,3
304,Cyril,20,900000,3
305,Yojith,20,900000,3
2 of 2
2

I recommend just using libraries because they're usually very optimised. I'll talk about that later. For now, here's a way that utilises the xml.dom.minidom module, which is a part of the Python standard library, so no additional libraries are required.

Edit: rewrote the last part using the standard CSV library instead of manually writing the file, as suggested by a comment. That makes for 2 Python built-in modules, not 1. The original code for the CSV writing will be at the end of the reply, if you're interested.

from xml.dom import minidom
from csv import DictWriter

# Step 1: Read and parse the XML file
# Write it as a string, or open the file and read it
xml_file = open('employees.xml', 'r')
xml_data = xml_file.read()

dom = minidom.parseString(xml_data)
employees = dom.getElementsByTagName('employee')

xml_file.close()

# Step 2: Extract the required information
data = []
for employee in employees:
    emp_data = {}
    for child in employee.childNodes:
        if child.nodeType == minidom.Node.ELEMENT_NODE:
            emp_data[child.tagName] = child.firstChild.data
    data.append(emp_data)

# Step 3: Write the extracted information to a CSV file
with open('output.csv', 'w', newline = '') as csv_file:
    fieldnames = ['id', 'name', 'age', 'salary', 'division']
    writer = DictWriter(csv_file, fieldnames = fieldnames)

    writer.writeheader()
    for emp_data in data:
        writer.writerow(emp_data)


Don't reinvent the wheel, just realign it.

— Anthony J. D'Angelo, I think

I recommend NOT using this code. You should really just use lxml. It's extremely simple and easy to use and can handle complex XML structures with nested elements and attributes. Let me know how everything goes!


Original CSV write code without CSV library
# Step 3: Write the extracted information to a CSV file
with open('output.csv', 'w') as f:
    f.write('id,name,age,salary,division\n')
    for emp_data in data:
        f.write(f"{emp_data['id']},{emp_data['name']},{emp_data['age']},{emp_data['salary']},{emp_data['division']}\n")
🌐
Python.org
discuss.python.org › python help
Convert xml to excel/csv - Python Help - Discussions on Python.org
October 15, 2022 - Please help me in converting XML file into excel/csv. Thank you in advance.
🌐
Teleport
goteleport.com › resources › tools › xml-to-csv-converter
XML to CSV Converter | Instantly Transform XML to CSV | Teleport
Convert XML data to CSV instantly with our free online tool. Simplify your data conversions with fast, accurate processing.
🌐
Wondershare PDF
pdf.wondershare.com › home › pdf converter › how to convert xml to csv in 5 easy ways
How to Convert XML to CSV in 5 Easy Ways
January 6, 2026 - You can convert XML to CSV using ElementTree module of the in-built xml module in Python. It can parse an XML document as a parameter and save the file in the form of tree, which has a getroot() method that returns the root element of the tree.
Find elsewhere
🌐
Plain English
plainenglish.io › home › blog › python › convert xml to csv using python
Convert XML to CSV Using Python
February 17, 2022 - #2 # Reading xml file with open("sample.xml", 'r') as file: filedata = file.read() # Converting xml to python dictionary (ordered dict) data_dict = xmltodict.parse(filedata)
🌐
SysTools Group
systoolsgroup.com › home › how to convert xml to csv file? 5 easy methods
Convert XML to CSV Format in Bulk Using the Best Five Methods
October 8, 2025 - Open any online converter. Upload your XML files. Choose “CSV” as a saving option. Select the path as per your choice. And hit on the “Convert” icon to convert XML to CSV format.
🌐
GitHub
github.com › knadh › xmlutils.py
GitHub - knadh/xmlutils.py: Python scripts for processing XML documents and converting to SQL, CSV, and JSON [UNMAINTAINED]
Simple table-representing XMLs can be converted to CSV using xmltable2csv. It assumes each entry is encapsulated in some tag, and successfuly tested on some XLSX files. Blind conversion of XML to CSV and SQL is not recommended.
Starred by 255 users
Forked by 141 users
Languages   Python 100.0% | Python 100.0%
🌐
Medium
medium.com › @ayushnandanwar003 › simplifying-data-conversion-a-comprehensive-guide-to-converting-xml-to-csv-in-python-b22c24b02628
Simplifying Data Conversion: A Comprehensive Guide to Converting XML to CSV in Python | by Ayush Nandanwar | Medium
April 26, 2024 - To convert XML data to CSV format in Python, we’ll leverage the power of two core modules: xml.etree.ElementTree for parsing XML and csv for handling CSV files.
🌐
Medium
medium.com › analytics-vidhya › converting-xml-data-to-csv-format-using-python-3ea09fa18d38
Converting XML data to CSV format using Python | by Pralhad Teggi | Analytics Vidhya | Medium
November 20, 2019 - The xml dump has huge number of events and for every event, I would like to extract an important attributes like title of the event, description, state, solution, severity, time of the event creation etc. This is achieved by the below code snippet. ... The code checks whether the extracted field is None and the extracted data is appended to a list. Once the extraction is completed, the list is written to the csv file as below.
🌐
Aspose
products.aspose.com › aspose.cells › python via .net › conversion › xml to csv
Python XML to CSV - XML to CSV Converter | products.aspose.com
November 13, 2025 - Add a library reference (import the library) to your Python project. Load XML file with an instance of Workbook. Convert XML to CSV by calling Workbook.save method.
🌐
Medium
medium.com › @meiyee715 › converting-xml-to-csv-python-xml-etree-25fec8e72626
Converting XML to CSV: Python xml.etree | by Amy Leong | Medium
October 14, 2023 - Replace path_to_your_xml_file.xml and path_to_output.csv with your desired paths. The provided script is a basic example, and real-world XML files can vary widely in their structure. Depending on the nature of the XML, you may need to account for attributes, nested elements, and other complexities. The beauty of Python is that it offers the flexibility to handle these scenarios with a bit more logic.
🌐
Delft Stack
delftstack.com › home › howto › python › xml to csv python
How to Convert XML to CSV Using Python | Delft Stack
July 22, 2022 - Finally, we use the to_csv() method ... written into the CSV file. xmltodict is a Python library that provides a convenient way to work with XML data in a more Pythonic and dictionary-like manner....
🌐
GroupDocs Cloud
blog.groupdocs.cloud › groupdocs cloud blog › convert xml to csv and csv to xml in python
Convert XML to CSV and CSV to XML in Python
November 1, 2022 - How to Convert CSV to XML in Python using REST API · For converting XML to CSV online, or vice versa, I will be using Python SDK of GroupDocs.Conversion Cloud API. This Python document conversion library is a platform-independent open-source library and file format conversion service.
🌐
Saturn Cloud
saturncloud.io › blog › converting-complex-xml-files-to-pandas-dataframecsv-in-python
Converting Complex XML Files to Pandas DataFrame/CSV in Python | Saturn Cloud Blog
December 28, 2023 - import xml.etree.ElementTree as ET import pandas as pd def parse_xml(xml_file): tree = ET.parse(xml_file) root = tree.getroot() return root def extract_data(root): data = [] for record in root.findall('record'): row = {} for field in record: row[field.tag] = field.text data.append(row) return data def to_dataframe(data): df = pd.DataFrame(data) return df def to_csv(df, filename): df.to_csv(filename, index=False) def main(xml_file, csv_file): root = parse_xml(xml_file) data = extract_data(root) # OR extract_data_custom(root, 'Customer') or `Order` df = to_dataframe(data) to_csv(df, csv_file) if
🌐
Stack Overflow
stackoverflow.com › questions › 74772537 › how-to-convert-xml-file-to-csv-using-python-script
how to convert xml file to csv using python script - Stack Overflow
import xml.etree.ElementTree as ET import csv # PARSE XML xml = ET.parse("./error.xml") root = xml.getElementsByTagName() # CREATE CSV FILE csvfile = open("data.csv",'w',encoding='utf-8') csvfile_writer = csv.writer(csvfile) # ADD THE HEADER TO CSV FILE csvfile_writer.writerow(["identifier","file","errorStyle","msg"]) # FOR EACH EMPLOYEE for error in root.findall("errors/error"): if(error): # EXTRACT EMPLOYEE DETAILS identifier = error.get('identifier') file = error.find('file') errorStyle = error.find("errorStyle") msg = error.find("msg") csv_line = [identifier, file.text, errorStyle.text, msg.text] # ADD A NEW ROW TO CSV FILE csvfile_writer.writerow(csv_line) csvfile.close()
🌐
JSON Formatter
jsonformatter.org › xml-to-csv
Best XML to CSV Converter Online
XML to CSV is very unique tool for convert JOSN to CSV and allows to download, save, share and print XML to CSV data..