It's worth looking at lxml.objectify.

xml = """<main>
<object1 attr="name">content</object1>
<object1 attr="foo">contenbar</object1>
<test>me</test>
</main>"""

from lxml import objectify

main = objectify.fromstring(xml)
main.object1[0]             # content
main.object1[1]             # contenbar
main.object1[0].get("attr") # name
main.test                   # me

Or the other way around to build xml structures:

item = objectify.Element("item")
item.title = "Best of python"
item.price = 17.98
item.price.set("currency", "EUR")

order = objectify.Element("order")
order.append(item)
order.item.quantity = 3
order.price = sum(item.price * item.quantity for item in order.item)

import lxml.etree
print(lxml.etree.tostring(order, pretty_print=True))

Output:

<order>
  <item>
    <title>Best of python</title>
    <price currency="EUR">17.98</price>
    <quantity>3</quantity>
  </item>
  <price>53.94</price>
</order>
Answer from Peter Hoffmann on Stack Overflow
๐ŸŒ
JSON Formatter
jsonformatter.org โ€บ xml-to-python
Best XML to Python Converter
XML to Python Online with https and easiest way to convert XML to Python. Save online and Share.
Discussions

Parse XML file into Python object - Stack Overflow
This code is from my teacher's Github. It converts XML string to Python object. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do I convert XML to HTML in python? - Stack Overflow
According to what I've found, a ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
A simple python3 library to convert JSON into XML via file, URL or string.
So basically dicttoxml.dicttoxml(json.loads(...)) ? More on reddit.com
๐ŸŒ r/Python
21
54
November 13, 2017
Converting .txt with RGB values to .xml or .qml?
Trying to convert a .txt to a .qml for use as a style for standard USGS lithologies. Any help would be appreciated :) More on reddit.com
๐ŸŒ r/QGIS
4
0
October 4, 2020
Top answer
1 of 2
3

If you have an appropriate XML Schema for these XML files, there are tools like GenerateDS which will generate python classes based on them.

That would allow you to load all of the files in memory, and have them as objects. How you then store that data elsewhere...well, you don't say what you want to do, but you could do anything you usually can with python.

2 of 2
1

Use the standard xml.etree.Element tree to extract the information from XML to Python objects (or the more enhanced third party lxml with the same API).

I recommend to read the Mark Pilrim's Dive Into Python 3, Chapter 12. XML (http://getpython3.com/diveintopython3/xml.html).

Here is the core of how the parser/compiler could be written. The idea is to go recursively through the elements, collect the neccessary information and output the code when it is possible:

import xml.etree.ElementTree as ET

class Parser:

    def __init__(self):
        self.output_list = []  # collected output lines
        self.il = 0            # indentation level


    def __iter__(self):
        return iter(self.output_list)


    def out(self, s):
        '''Output the indented string to the output list.'''
        self.output_list.append('    ' * self.il + s)


    def indent(self, num=1):
        '''Increase the indentation level.'''
        self.il += num


    def dedent(self, num=1):
        '''Decrease the indentation level.'''
        self.il -= num


    def parse(self, elem):
        '''Call the parser of the elem.tag name.

        The tag name appended to "parse_" and then the name of that
        function is called.  If the function is not defined, then
        self.parse_undefined() is called.'''

        fn_name = 'parse_' + elem.tag
        try:
            fn = getattr(self, fn_name)
        except AttributeError:
            fn = self.parse_undefined
        return fn(elem)


    def loop(self, elem):
        '''Helper method to loop through the child elements.'''
        for e in elem:
            self.parse(e)


    def parseXMLfile(self, fname):
        '''Reads the XML file and starts parsing from the root element.'''
        tree = ET.parse(fname)
        script = tree.getroot()
        assert script.tag == 'script'
        self.parse(script)


    ###################### ELEMENT PARSERS #######################

    def parse_undefined(self, elem):
        '''Called for the element that has no parser defined.'''
        self.out('PARSING UNDEFINED for ' + elem.tag)


    def parse_script(self, elem):
        self.loop(elem)


    def parse_stage(self, elem):
        self.out('')
        self.out('Parsing the stage: ' + elem.attrib['id'])
        self.indent()
        self.loop(elem)
        self.dedent()


    def parse_initialise(self, elem):
        self.out('')
        self.out('#---------- ' + elem.tag + ' ----------')
        self.loop(elem)


    def parse_variable(self, elem):
        tt = str   # default type
        if elem.attrib['type'] == 'Integer': 
            tt = int
        # elif ... etc for other types

        # Conversion of the value to the type because of the later repr().
        value = tt(elem.attrib['value'])  

        id_ = elem.attrib['id']

        # Produce the line of the output.
        self.out('{0} = {1}'.format(id_, repr(value)))


    def parse_execute(self, elem):
        self.out('')
        self.out('#---------- ' + elem.tag + ' ----------')
        self.loop(elem)


    def parse_if(self, elem):
        assert elem[0].tag == 'condition'
        condition = self.parse(elem[0])
        self.out('if ' + condition + ':')
        self.indent()
        self.loop(elem[1:])
        self.dedent()


    def parse_condition(self, elem):
        assert len(elem) == 0
        return elem.text


    def parse_then(self, elem):
        self.loop(elem)


    def parse_else(self, elem):
        self.dedent()
        self.out('else:')
        self.indent()
        self.loop(elem)


    def parse_error(self, elem):
        assert len(elem) == 0
        errorID = elem.attrib.get('errorID', None)
        fieldID = elem.attrib.get('fieldID', None)
        self.out('error({0}, {1})'.format(errorID, fieldID))


    def parse_setNextStage(self, elem):
        assert len(elem) == 0
        self.out('setNextStage --> ' + elem.text)


if __name__ == '__main__':
    parser = Parser()
    parser.parseXMLfile('data.xml')
    for s in parser:
        print s

When used with the data pasted here http://pastebin.com/vRRxfWiA, the script produces the following output:

Parsing the stage: stage1

    #---------- initialise ----------
    taxyear = 2012
    taxyearstart = '06/04/2012'
    taxyearend = '05/04/2013'
    previousemergencytaxcode = '747L'
    emergencytaxcode = '810L'
    nextemergencytaxcode = '810L'

    ...

    maxLimitAmount = 0
    PARSING UNDEFINED for executeMethod
    if $maxLimitReached$ == True:
        employeepayrecord = 'N'
        employeepayrecordstate = '2'
    else:
        employeepayrecordstate = '1'
    gender = ''
    genderstate = '1'
    title = ''
    forename = ''
    forename2 = ''
    surname = ''
    dob = ''
    dobinvalid = ''

    #---------- execute ----------
    if $dobstring$ != "":
        validDOBCheck = 'False'
        PARSING UNDEFINED for executeMethod
        if $validDOBCheck$ == False:
            error(224, dob)
        else:
            minimumDOBDate = ''
            PARSING UNDEFINED for executeMethod
            validDOBCheck = 'False'
            PARSING UNDEFINED for executeMethod
            if $validDOBCheck$ == False:
                error(3007161, dob)
        if $dobstring$ == "01/01/1901":
            error(231, dob)
    else:
        error(231, dob)

Parsing the stage: stage2

    #---------- initialise ----------
    address1 = ''

    ...
๐ŸŒ
Oโ€™Reilly Media
oreilly.com โ€บ o'reilly โ€บ radar โ€บ convert between xml and native python data structures with jxmlease
Convert between XML and native Python data structures with jxmlease
May 19, 2016 - I think we succeeded. jxmlease is a Python module that converts XML data into Python objects in a way that preserves the structure of the original XML data, while also maintaining the metadata.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ xml.etree.elementtree.html
xml.etree.ElementTree โ€” The ElementTree XML API
January 29, 2026 - Parses an XML section from a string constant. This function can be used to embed โ€œXML literalsโ€ in Python code. text is a string containing XML data. parser is an optional parser instance. If not given, the standard XMLParser parser is used.
๐ŸŒ
Sonra
sonra.io โ€บ home โ€บ xml โ€บ xml conversion using python in 2025
XML Conversion Using Python in 2026 - Sonra
January 14, 2025 - Python provides several libraries and methods suitable for XML conversion, catering to various data handling requirements. While XML is one of many formats used for data exchange and storage, its conversion into more commonly used formats like CSV can enhance data interoperability and integration. This overview introduces key Python libraries, including lxml, ElementTree, xmltodict, BeautifulSoup, and pandas, that facilitate the conversion process. These libraries offer diverse approaches for parsing, modifying, and converting XML.
Find elsewhere
Top answer
1 of 5
33

My beloved SD Chargers hat is off to you if you think a regex is easier than this:

#!/usr/bin/env python
import xml.etree.cElementTree as et

sxml="""
<encspot>
  <file>
   <Name>some filename.mp3</Name>
   <Encoder>Gogo (after 3.0)</Encoder>
   <Bitrate>131</Bitrate>
  </file>
  <file>
   <Name>another filename.mp3</Name>
   <Encoder>iTunes</Encoder>
   <Bitrate>128</Bitrate>  
  </file>
</encspot>
"""
tree=et.fromstring(sxml)

for el in tree.findall('file'):
    print '-------------------'
    for ch in el.getchildren():
        print '{:>15}: {:<30}'.format(ch.tag, ch.text) 

print "\nan alternate way:"  
el=tree.find('file[2]/Name')  # xpath
print '{:>15}: {:<30}'.format(el.tag, el.text)  

Output:

-------------------
           Name: some filename.mp3             
        Encoder: Gogo (after 3.0)              
        Bitrate: 131                           
-------------------
           Name: another filename.mp3          
        Encoder: iTunes                        
        Bitrate: 128                           

an alternate way:
           Name: another filename.mp3  

If your attraction to a regex is being terse, here is an equally incomprehensible bit of list comprehension to create a data structure:

[(ch.tag,ch.text) for e in tree.findall('file') for ch in e.getchildren()]

Which creates a list of tuples of the XML children of <file> in document order:

[('Name', 'some filename.mp3'), 
 ('Encoder', 'Gogo (after 3.0)'), 
 ('Bitrate', '131'), 
 ('Name', 'another filename.mp3'), 
 ('Encoder', 'iTunes'), 
 ('Bitrate', '128')]

With a few more lines and a little more thought, obviously, you can create any data structure that you want from XML with ElementTree. It is part of the Python distribution.

Edit

Code golf is on!

[{item.tag: item.text for item in ch} for ch in tree.findall('file')] 
[ {'Bitrate': '131', 
   'Name': 'some filename.mp3', 
   'Encoder': 'Gogo (after 3.0)'}, 
  {'Bitrate': '128', 
   'Name': 'another filename.mp3', 
   'Encoder': 'iTunes'}]

If your XML only has the file section, you can choose your golf. If your XML has other tags, other sections, you need to account for the section the children are in and you will need to use findall

There is a tutorial on ElementTree at Effbot.org

2 of 5
8

Use ElementTree. You don't need/want to muck about with a parse-only gadget like pyexpat ... you'd only end up re-inventing ElementTree partially and poorly.

Another possibility is lxml which is a third-party package which implements the ElementTree interface plus more.

Update Someone started playing code-golf; here's my entry, which actually creates the data structure you asked for:

# xs = """<encspot> etc etc </encspot"""
>>> import xml.etree.cElementTree as et
>>> from pprint import pprint as pp
>>> pp([dict((attr.tag, attr.text) for attr in el) for el in et.fromstring(xs)])
[{'Bitrate': '131',
  'Encoder': 'Gogo (after 3.0)',
  'Frame': 'no',
  'Frames': '6255',
  'Freq.': '44100',
  'Length': '00:02:43',
  'Mode': 'joint stereo',
  'Name': 'some filename.mp3',
  'Quality': 'good',
  'Size': '5,236,644'},
 {'Bitrate': '0', 'Name': 'foo.mp3'}]
>>>

You'd probably want to have a dict mapping "attribute" names to conversion functions:

converters = {
    'Frames': int,
    'Size': lambda x: int(x.replace(',', '')),
    # etc
    }
๐ŸŒ
GitHub
github.com โ€บ knadh โ€บ xmlutils.py
GitHub - knadh/xmlutils.py: Python scripts for processing XML documents and converting to SQL, CSV, and JSON [UNMAINTAINED]
Python scripts for processing XML documents and converting to SQL, CSV, and JSON [UNMAINTAINED] - knadh/xmlutils.py
Starred by 255 users
Forked by 141 users
Languages ย  Python 100.0% | Python 100.0%
๐ŸŒ
Edureka
edureka.co โ€บ blog โ€บ python-xml-parser-tutorial
How to Parse and Modify XML in Python?
December 5, 2024 - Here are some popular third-party XML parser libraries: ... โ€“ Purpose: untangle is a Python library that simplifies XML parsing by directly converting XML data into Python objects.
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ converting-xml-to-python-dataframe-a-comprehensive-guide
Converting XML to Python DataFrame: A Guide | Saturn Cloud Blog
November 15, 2023 - And there you have it! Your XML data is now in a Python DataFrame, ready for analysis. Converting XML to a Python DataFrame can be a bit tricky, but with the right approach, it becomes a straightforward task. This guide has shown you how to parse an XML file, extract the necessary data, and convert it into a DataFrame using pandas.
๐ŸŒ
The Hitchhiker's Guide to Python
docs.python-guide.org โ€บ scenarios โ€บ xml
XML parsing โ€” The Hitchhiker's Guide to Python
from xmlschema import XMLSchema, etree_tostring # load a XSD schema file schema = XMLSchema("your_schema.xsd") # validate against the schema schema.validate("your_file.xml") # or schema.is_valid("your_file.xml") # decode a file data = ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-convert-lists-to-xml-in-python
How to convert lists to XML in Python? - GeeksforGeeks
July 23, 2025 - DOM (document object model) may be a cross-language API from W3C i.e. World Wide Web Consortium for accessing and modifying XML documents. Python enables you to parse XML files with the assistance of xml.dom.minidom, which is that the minimal implementation of the DOM interface.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ convert-xml-to-csv-in-python
Convert XML to CSV in Python - GeeksforGeeks
July 23, 2025 - In this article, we will explore how to convert XML to CSV step-by-step with the help of the built-in xml.etree.ElementTree module and the powerful pandas library. Import necessary libraries. Define the desired column headers.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-parse-xml-in-python-without-using-external-libraries
How to Parse XML in Python Without Using External Libraries
November 12, 2025 - We convert numeric strings to proper types (float for price, int for stock) For nested categories, we first check if the <categories> element exists. Then we iterate through child <category> elements and collect their text ยท The result is clean Python data structures you can easily work with. You can now use the parser like so: products = parse_product_catalog('products.xml') for product in products: print(f"\nProduct: {product['name']}") print(f" ID: {product['id']}") print(f" Price: {product['currency']} {product['price']}") print(f" Stock: {product['stock']}") print(f" Categories: {', '.join(product['categories'])}")
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-xml-to-json-dict
Python XML to JSON, XML to Dict | DigitalOcean
August 3, 2022 - Today we will learn how to convert XML to JSON and XML to Dict in python. We can use python xmltodict module to read XML file and convert it to Dict or JSON data. We can also stream over large XML files and convert them to Dictionary.
๐ŸŒ
Dive into Python
diveintopython.org โ€บ home โ€บ learn python programming โ€บ file handling and file operations โ€บ xml files handling
XML File Operations with Python - Read, Write and Parse XML Data
May 3, 2024 - The articles describes how you can open and read XML files using Python. Code examples show you how to convert XML data to CSV format as well.
๐ŸŒ
Quora
quora.com โ€บ How-can-I-convert-XML-to-JSON-in-Python
How to convert XML to JSON in Python - Quora
Convert XML to a dict. Convert list to JSON. A well-liked Python package called Xmltodict may change your XML structure into a JSON format. It makes dealing with XML easy and gives you the impression that you are using JSON.
๐ŸŒ
Aspose
products.aspose.com โ€บ aspose.cells โ€บ python via .net โ€บ conversion โ€บ xml to txt
Convert XML to TXT in Python Excel Library - Conversion
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 TXT by calling Workbook.save method.