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
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")
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-xml-to-csv-in-python
Convert XML to CSV in Python - GeeksforGeeks
July 23, 2025 - We used ElementTree to parse and navigate through the XML structure. Data from each record was collected into a list of dictionaries. Finally, we used pandas to create a CSV file from that structured data. To learn about the pandas module in depth, refer to: Python Pandas Tutorial
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
Converting CSV file to XML
Excel can export data in an XML format. Open it in Excel and simply export it, no programming needed. I believe you do need to enable developer mode. More on reddit.com
🌐 r/CodingHelp
6
1
September 8, 2024
Parsing large XML into CSV?
You need an out-of-core xml converter that reads in a chunk of the xml file, writes it out as csv line(s), then reads in the next chunk. I just finished writing a proprietary one for converting really large ecommerce product catalogs for my company. It took about a week of effort on my part to create. (I did this because I haven't had good luck getting LXML to stay low-memory without hitting some bugs in it.) These are the pieces I wrote to make it work: An analysis function to read through the head of the file as regular text to analyze if it contains a known xml tags to separate the product records or unknown tags of sufficient frequency to be used (each product record contains multiple sub elements with properties and values). A generator function that yields one sku record's worth of xml text at a time by looping through the xml text of the whole file in chunks (I'm using 256k chunks because that has good performance for me) An xml interpreter that takes the text from a single sku record and breaks it down into a nested set of xml element class objects with properties and values. A method of the xml element class that converts the properities and values of the nested objects of a single product into a flat dictionary A function to convert the flat dictionary into an ordered list of cells to write to a csv A function to read through either the first 500 rows of the xml file or through the whole file to find the columns names to use for the csv A wrapper class to operates as an interator for easy access. It can operate like this: xml_iterator_object = XmlFile(xml_path_or_buffer) for row_cells in xml_iterator_object: write_csv_row(row_cells) I cythonized the majority of the internal functions for speed. More on reddit.com
🌐 r/learnpython
2
2
April 16, 2018
How do I convert XML into a CSV in the most efficient way possible?
How To Parse and Convert XML to CSV using Python . More on reddit.com
🌐 r/datascience
7
3
July 5, 2017
🌐
Delft Stack
delftstack.com › home › howto › python › xml to csv python
How to Convert XML to CSV Using Python | Delft Stack
February 2, 2024 - It is a high-performance, production-quality library that is easy to use and implements the ElementTree API, a Pythonic binding for the C libraries libxml2 and libxslt. It is feature-rich and includes support for XPath, XSLT, Relax NG, and more. Ensure that you have the lxml library installed. If not, you can install it via pip: ... We will parse the same XML data using lxml, extract the required data, and then use the csv library to write this data into a CSV file.
🌐
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.
🌐
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 - Aspose Excel. This comprehensive solution provides Python developers with a fully integrated approach to convert XML to CSV format, enabling seamless saving of XML data into CSV format using the Aspose.Cells library, all through efficient and ...
🌐
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.
Find elsewhere
🌐
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
🌐
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.
🌐
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 - 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. It is 100% free, secure and easy to use Python library for converting file formats.
🌐
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 - Data transformation doesn’t have to be a daunting task. With Python at our disposal, we’ve managed to transform XML to CSV in just a few lines of code. This exercise is more than just a coding task; it’s a testament to Python’s capability to simplify complex operations and make data more accessible.
🌐
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 - Do you also have nested and large XML files, and you are confused about how to convert them into CSV format? If it sounds right and accurate, then you have opened the right blog. Users can easily convert XML to CSV format using Notepad++, Python Script, Microsoft Excel, and an alternate automatic solution.
🌐
Convertio
convertio.com › xml to csv
XML to CSV Converter — Free Online - Convertio.com
Convert XML files to CSV online for free. Flatten hierarchical data into spreadsheet-ready tables. No software needed.
🌐
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.
🌐
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.
🌐
Reddit
reddit.com › r/codinghelp › converting csv file to xml
r/CodingHelp on Reddit: Converting CSV file to XML
September 8, 2024 -

I don't know if this is the right place to post this, but I'm looking for someone who can create a process to convert a daily csv file to xml that can be imported into our billing software.

I'm just looking for a way to find someone to pay to get this set up for me (the company I work for). I've tried searching and have come up empty, so am reaching out to reddit where I get all of my questions answered!

To sum up, I'm looking for where I would search to find someone to write a program / process for me to use to convert a csv file into xml to be able to be imported into a billing software program.

Thanks!

🌐
Sonra
sonra.io › home › xml › xml conversion using python in 2025
XML Conversion Using Python in 2026 - Sonra
January 14, 2025 - XML (eXtensible Markup Language) ... away in XML needs to be converted to a more suitable format, typically a relational database or CSV. One option to convert XML is Python....
🌐
Sonra
sonra.io › home › xml › convert xml to csv like a pro – essential tools & methods
Convert XML to CSV Like a Pro – Essential Tools & Methods
March 22, 2025 - You can refer to our guide XML conversion in Python where we compare the various options and libraries such as Element Tree and lxml of converting XML to CSV.
🌐
Softaken
softaken.com › home › how to convert xml to csv file with complete accuracy
Convert XML to CSV with Complete Accuracy | Easy Guide
February 7, 2026 - Step 1: Firstly, open Python and copy-paste the code into it. Step 2: Once the code has been pasted choose Run from the top bar. The resultant file will be saved in the same folder as this script in this manner.
🌐
GitHub
gist.github.com › 6cde231bbd9bed2c99677c0e9cc5c3b3
Convert XML to CSV and CSV to XML in Python · GitHub
You can convert XML file to CSV and convert CSV to XML online in Python programmatically on the cloud.