๐ŸŒ
PyPI
pypi.org โ€บ project โ€บ xmltodict
xmltodict ยท PyPI
>>> import xmltodict >>> >>> mydict = { ... 'text': { ... '@color':'red', ... '@stroke':'2', ... '#text':'This is a test' ...
      ยป pip install xmltodict
    
Published ย  Feb 22, 2026
Version ย  1.0.4
๐ŸŒ
Readthedocs
xmltodict.readthedocs.io โ€บ en โ€บ stable โ€บ README
README - xmltodict
>>> import xmltodict >>> >>> mydict = { ... 'text': { ... '@color':'red', ... '@stroke':'2', ... '#text':'This is a test' ...
๐ŸŒ
Omz Software
omz-software.com โ€บ pythonista โ€บ docs โ€บ ios โ€บ xmltodict.html
xmltodict โ€” Python 3.6.1 documentation
February 19, 2020 - >>> def postprocessor(path, key, value): ... try: ... return key + ':int', int(value) ... except (ValueError, TypeError): ... return key, value >>> xmltodict.parse('<a><b>1</b><b>2</b><b>x</b></a>', ... postprocessor=postprocessor) OrderedDict([(u'a', OrderedDict([(u'b:int', [1, 2]), (u'b', u'x')]))]) You can pass an alternate version of expat (such as defusedexpat) by using the expat parameter. E.g: >>> import defusedexpat >>> xmltodict.parse('<a>hello</a>', expat=defusedexpat.pyexpat) OrderedDict([(u'a', u'hello')])
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ xmltodict module in python: a practical reference
xmltodict Module in Python: A Practical Reference - AskPython
December 9, 2020 - xmltodict.parse() method takes an XML file as input and changes it to Ordered Dictionary. We can then extract the dictionary data from Ordered Dictionary using dict constructor for Python dictionaries.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-xml-to-json-dict
Python XML to JSON, XML to Dict | DigitalOcean
August 3, 2022 - As the module name suggest itself, xmltodict actually converts the XML data we provide to just a simply Python dictionary. So, we can simply access the data with the dictionary keys as well. Here is a sample program: import xmltodict import pprint import json my_xml = """ <audience> <id ...
๐ŸŒ
GitHub
github.com โ€บ martinblech โ€บ xmltodict โ€บ blob โ€บ master โ€บ xmltodict.py
xmltodict/xmltodict.py at master ยท martinblech/xmltodict
>>> import xmltodict ยท >>> doc = xmltodict.parse(\"\"\" ... <a prop="x"> ... <b>1</b> ... <b>2</b> ... </a> ... \"\"\") >>> doc['a']['@prop'] 'x' >>> doc['a']['b'] ['1', '2'] ยท
Author ย  martinblech
๐ŸŒ
GitHub
github.com โ€บ martinblech โ€บ xmltodict
GitHub - martinblech/xmltodict: Python module that makes working with XML feel like you are working with JSON ยท GitHub
February 16, 2014 - >>> import xmltodict >>> >>> mydict = { ... 'text': { ... '@color':'red', ... '@stroke':'2', ... '#text':'This is a test' ...
Starred by 5.7K users
Forked by 468 users
Languages ย  Python
๐ŸŒ
The Hitchhiker's Guide to Python
python-docs.readthedocs.io โ€บ en โ€บ latest โ€บ scenarios โ€บ xml.html
XML parsing - The Hitchhiker's Guide to Python - Read the Docs
import xmltodict with open('path/to/file.xml') as fd: doc = xmltodict.parse(fd.read()) and then you can access elements, attributes and values like this: doc['mydocument']['@has'] # == u'an attribute' doc['mydocument']['and']['many'] # == [u'elements', u'more elements'] doc['mydocument']['...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-program-to-convert-xml-to-dictionary
Python program to convert XML to Dictionary - GeeksforGeeks
September 5, 2024 - # Import the required modules import xmltodict import pprint # Open the file and read the contents with open('example_2.xml', 'r', encoding='utf-8') as file: my_xml = file.read() # Use xmltodict to parse and convert the # XML document my_dict ...
Find elsewhere
๐ŸŒ
Tanium
tanium.github.io โ€บ pytan โ€บ _modules โ€บ xmltodict.html
xmltodict โ€” PyTan v2.1.6 2.1.6 documentation
If `xml_attribs` is `True`, element attributes are put in the dictionary among regular child elements, using `@` as a prefix to avoid collisions. If set to `False`, they are just ignored. Simple example:: >>> import xmltodict >>> doc = xmltodict.parse(\"\"\" ...
Top answer
1 of 4
36

Using your example:

import xmltodict

with open('artikelen.xml') as fd:
    doc = xmltodict.parse(fd.read())

If you examine doc, you'll see it's an OrderedDict, ordered by tag:

>>> doc
OrderedDict([('artikelen',
              OrderedDict([('artikel',
                            [OrderedDict([('@nummer', '121'),
                                          ('code', 'ABC123'),
                                          ('naam', 'Highlight pen'),
                                          ('voorraad', '231'),
                                          ('prijs', '0.56')]),
                             OrderedDict([('@nummer', '123'),
                                          ('code', 'PQR678'),
                                          ('naam', 'Nietmachine'),
                                          ('voorraad', '587'),
                                          ('prijs', '9.99')])])]))])

The root node is called artikelen, and there is a subnode artikel which is a list of OrderedDict objects, so if you want the code for every article, you would do:

codes = []
for artikel in doc['artikelen']['artikel']:
    codes.append(artikel['code'])

# >>> codes
# ['ABC123', 'PQR678']

If you specifically want the code only when nummer is 121, you could do this:

code = None
for artikel in doc['artikelen']['artikel']:
    if artikel['@nummer'] == '121':
        code = artikel['code']
        break

That said, if you're parsing XML documents and want to search for a specific value like that, I would consider using XPath expressions, which are supported by ElementTree.

2 of 4
-1

This is using xml.etree You can try this:

for artikelobj in root.findall('artikel'):
    print artikelobj.find('code')

if you want to extract a specific code based on the attribute 'nummer' of artikel, then you can try this:

for artikelobj in root.findall('artikel'):
    if artikel.get('nummer') == 121:
        print artikelobj.find('code')

this will print only the code you want.

๐ŸŒ
Medium
cihankursun95.medium.com โ€บ how-to-parse-xmltodict-and-write-elementtree-xml-with-using-python-33361ca68aba
How to Parse (xmltodict) and Write (ElementTree) XML with Using Python? | by cihan kursun | Medium
May 21, 2022 - The doc variable here allows parsing the read xml file using the xmltodict library. I will add the pdb library to get output quickly and check the variable I assigned by throwing a debug at the end of the code I wrote. import xmltodict import pdb with open('example.xml') as fd: doc = xmltodict.parse(fd.read()) pdb.set_trace() print("xml is read")
๐ŸŒ
The Hitchhiker's Guide to Python
docs.python-guide.org โ€บ scenarios โ€บ xml
XML parsing โ€” The Hitchhiker's Guide to Python
import xmltodict with open('path/to/file.xml') as fd: doc = xmltodict.parse(fd.read()) and then you can access elements, attributes, and values like this: doc['mydocument']['@has'] # == u'an attribute' doc['mydocument']['and']['many'] # == ...
๐ŸŒ
Calazan
calazan.com โ€บ easily-convert-xml-data-to-a-python-dict-with-xmltodict
Easily convert XML data to a Python dict with xmltodict | Calazan.com
September 1, 2019 - Let's parse this and convert it to a dict using xmltodict: import xmltodict xmltodict.parse(xml_data) Yup, it's a one-liner.
๐ŸŒ
Snyk
snyk.io โ€บ advisor โ€บ xmltodict โ€บ functions โ€บ xmltodict.parse
How to use the xmltodict.parse function in xmltodict | Snyk
""" dir_out_dict = xmltodict.parse(self.device.show('dir', text=True)[1]) dir_out = dir_out_dict['ins_api']['outputs']['output']['body'] match = re.search(r'(\d+) bytes free', dir_out) bytes_free = match.group(1) return int(bytes_free) CiscoDevNet / netprog_basics / network_device_apis / netconf / example2.py View on Github ยท import xmltodict # NETCONF filter to use netconf_filter = open("filter-ietf-interfaces.xml").read() if __name__ == '__main__': with manager.connect(host=ios_xe1["address"], port=ios_xe1["port"], username=ios_xe1["username"], password=ios_xe1["password"], hostkey_verify=F
๐ŸŒ
MicroPyramid
micropyramid.com โ€บ blog โ€บ how-to-convert-xml-content-into-json-using-xmltodict
How to Convert XML Content into Json Using XMLtodict | MicroPyramid
>>> import xmltodict >>> import json >>> json.loads(json.dumps(xmltodict.parse(''' <root> <persons city="hyderabad"> <person name="abc"> <name age="50" mobile="789" /> </person> </persons> <persons city="vizag"> <person name="xyz"> <name age="70" mobile="123" /> </person> </persons> </root> ''', process_namespaces=True))) >>> mydict = {u'root': {u'persons': [{u'@city': u'hyderabad', u'person': {u'@name': u'abc', u'name': {u'@mobile': u'789', u'@age': u'50'}}}, {u'@city': u'vizag', u'person': {u'@name': u'xyz', u'name': {u'@mobile': u'123', u'@age': u'70'}}}]}} >>> print xmltodict.unparse(mydict, pretty=True) To Know more about our Django CRM(Customer Relationship Management) Open Source Package.
๐ŸŒ
Anaconda.org
anaconda.org โ€บ conda-forge โ€บ xmltodict
xmltodict - conda-forge | Anaconda.org
February 22, 2026 - Install xmltodict with Anaconda.org. Makes working with XML feel like you are working with JSON