I think I found the right syntax (it seems to be working as I expected):

response = xmltodict.parse(xml, force_list=('Child','Brother'))

Just posting in case anyone would look for the same answer in the future.

Answer from Phileas on Stack Overflow
🌐
PyPI
pypi.org › project › xmltodict
xmltodict · PyPI
>>> xml = '<a><b>data1</b><c>data2</c><d>data3</d></a>' >>> # Force CDATA only for 'b' and 'd' elements >>> xmltodict.parse(xml, force_cdata=('b', 'd')) {'a': {'b': {'#text': 'data1'}, 'c': 'data2', 'd': {'#text': 'data3'}}} >>> # Force CDATA for all elements (original behavior) >>> xmltodict.parse(xml, force_cdata=True) {'a': {'b': {'#text': 'data1'}, 'c': {'#text': 'data2'}, 'd': {'#text': 'data3'}}} >>> # Use a callable for complex logic >>> def should_force_cdata(path, key, value): ... return key in ['b', 'd'] and len(value) > 4 >>> xmltodict.parse(xml, force_cdata=should_force_cdata) {'a': {'b': {'#text': 'data1'}, 'c': 'data2', 'd': {'#text': 'data3'}}} The force_list parameter can be used to selectively force list values for specific elements:
      » pip install xmltodict
    
Published   Feb 22, 2026
Version   1.0.4
🌐
GitHub
github.com › martinblech › xmltodict › blob › master › xmltodict.py
xmltodict/xmltodict.py at master · martinblech/xmltodict
>>> xmltodict.parse('<a>hello</a>', ... there is only a single child of a given level of hierarchy. The · force_list argument is a tuple of keys....
Author   martinblech
🌐
Snyk
snyk.io › advisor › xmltodict › functions › xmltodict.parse
How to use the xmltodict.parse function in xmltodict | Snyk
def __list_flows( api_call: str, output_format: str = 'dict' ) -> Union[Dict, pd.DataFrame]: xml_string = openml._api_calls._perform_api_call(api_call, 'get') flows_dict = xmltodict.parse(xml_string, force_list=('oml:flow',)) # Minimalistic check if the XML is useful assert type(flows_dict['oml:flows']['oml:flow']) == list, \ type(flows_dict['oml:flows']) assert flows_dict['oml:flows']['@xmlns:oml'] == \ 'http://openml.org/openml', flows_dict['oml:flows']['@xmlns:oml'] flows = dict() for flow_ in flows_dict['oml:flows']['oml:flow']: fid = int(flow_['oml:id']) flow = {'id': fid, 'full_name': flow_['oml:full_name'], 'name': flow_['oml:name'], 'version': flow_['oml:version'], 'external_version': flow_['oml:external_version'], 'uploader': flow_['oml:uploader']} openml / openml-python / openml / tasks / functions.py View on Github ·
🌐
Omz Software
omz-software.com › pythonista › docs › ios › xmltodict.html
xmltodict — Python 3.6.1 documentation
February 19, 2020 - Parse the given XML input and convert it into a dictionary. xml_input can either be a string or a file-like object. 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.
🌐
Readthedocs
warriorframework.readthedocs.io › en › latest › _modules › Framework › OSS › xmltodict.html
Framework.OSS.xmltodict — warriorframework documentation
Usage example:: >>> 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')]) You can use the force_list argument to force lists to be created even when there is only a single child of a given level of hierarchy.
Find elsewhere
🌐
GitHub
github.com › martinblech › xmltodict › issues › 243
Document force_list in README · Issue #243 · martinblech/xmltodict
April 17, 2020 - I was encountering some issues where I needed to ensure that some entries in XML would always get forced into a list and eventually found from the xmltodict.parse docstring that there is a force_list keyword argument to do this. It would...
Author   mivade
🌐
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 - >>> xml = '<a><b>data1</b><c>data2</c><d>data3</d></a>' >>> # Force CDATA only for 'b' and 'd' elements >>> xmltodict.parse(xml, force_cdata=('b', 'd')) {'a': {'b': {'#text': 'data1'}, 'c': 'data2', 'd': {'#text': 'data3'}}} >>> # Force CDATA for all elements (original behavior) >>> xmltodict.parse(xml, force_cdata=True) {'a': {'b': {'#text': 'data1'}, 'c': {'#text': 'data2'}, 'd': {'#text': 'data3'}}} >>> # Use a callable for complex logic >>> def should_force_cdata(path, key, value): ... return key in ['b', 'd'] and len(value) > 4 >>> xmltodict.parse(xml, force_cdata=should_force_cdata) {'a': {'b': {'#text': 'data1'}, 'c': 'data2', 'd': {'#text': 'data3'}}} The force_list parameter can be used to selectively force list values for specific elements:
Starred by 5.7K users
Forked by 468 users
Languages   Python
🌐
Stack Overflow
stackoverflow.com › tags › xmltodict › hot
Hottest 'xmltodict' Answers - Stack Overflow
I think I found the right syntax (it seems to be working as I expected): response = xmltodict.parse(xml, force_list=('Child','Brother')) Just posting in case anyone would look for the same answer in ...
🌐
CloudDefense.ai
clouddefense.ai › code › python › example › xmltodict
Top 10 Examples of <!-- -->xmltodict<!-- --> code in Python | CloudDefense.AI
def __list_tasks(api_call, output_format='dict'): xml_string = openml._api_calls._perform_api_call(api_call, 'get') tasks_dict = xmltodict.parse(xml_string, force_list=('oml:task', 'oml:input')) # Minimalistic check if the XML is useful if 'oml:tasks' not in tasks_dict: raise ValueError('Error in return XML, does not contain "oml:runs": %s' % str(tasks_dict)) elif '@xmlns:oml' not in tasks_dict['oml:tasks']: raise ValueError('Error in return XML, does not contain ' '"oml:runs"/@xmlns:oml: %s' % str(tasks_dict)) elif tasks_dict['oml:tasks']['@xmlns:oml'] != 'http://openml.org/openml': raise ValueError('Error in return XML, value of ' '"oml:runs"/@xmlns:oml is not ' '"http://openml.org/openml": %s' % str(tasks_dict)) assert type(tasks_dict['oml:tasks']['oml:task']) == list, \
🌐
ProgramCreek
programcreek.com › python › example › 82408 › xmltodict.parse
Python Examples of xmltodict.parse
def __init__(self, file_name): self.data = [] content = open(file_name, encoding="utf-8").read() is_test_file = file_name.split("/")[-1] == "testdata_with_lex.xml" structure = xmltodict.parse(content) for i, entry in enumerate(structure["benchmark"]["entries"]["entry"]): triplets = [tuple(map(str.strip, r.split("|"))) for r in self.triplets_from_object(entry["modifiedtripleset"], "mtriple")] sentences = list(self.extract_sentences(entry["lex"])) for s in sentences: info = { "id": i, "seen": not is_test_file or i <= 970, "manual": is_test_file and i + 1 in FOR_MANUAL_EVAL and i <= 970 } self.data.append(Datum(rdfs=triplets, text=s, info=info))
🌐
GitHub
github.com › martinblech › xmltodict › issues › 14
xml containing 1 child · Issue #14 · martinblech/xmltodict
February 2, 2013 - xml = """<?xml version="1.0" encoding="utf-8" ?> <root> <children> <child> <name>A</name> </child> </children> </root>""" xmltodict.parse(xml)['root']['children']['child'] Wouldn't you expect to have an iterable object even when there is only 1 child? Reactions are currently unavailable ·
Author   ocZio
🌐
AskPython
askpython.com › home › xmltodict module in python: a practical reference
xmltodict Module in Python: A Practical Reference - AskPython
December 9, 2020 - We can convert XML data into JSON format using the xmltodict module and the json module in python. In this process, we first create an ordered dictionary from XML format using xmltodict.parse() method.
🌐
Stack Overflow
stackoverflow.com › questions › 74519251 › xmltodict-force-list-for-duplicated-key
json - xmltodict force_list for duplicated key - Stack Overflow
November 21, 2022 - the following enforces the list to all Child_A: import json import xmltodict res = xmltodict.parse(xml, force_list={'Child_A'}) print(json.dumps(res, indent=2, sort_keys=True)) { "Header_A": { "Child_A": [ null ], "Header_B": { "Header_C": { "Child_A": [ null ] } } } } json ·
🌐
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
xmltodict also lets you roundtrip back to XML with the unparse function, has a streaming mode suitable for handling files that don’t fit in memory and supports namespaces. This opinionated guide exists to provide both novice and expert Python developers a best practice handbook to the installation, configuration, and usage of Python on a daily basis. Receive updates on new releases and upcoming projects. ... Say Thanks! Join Mailing List...