xmltodict icon indicating copy to clipboard operation
xmltodict copied to clipboard

Improvement of the documentation for force_list when parsing XML with xmltodict

Open ByteEngeneer opened this issue 1 year ago • 1 comments

When parsing XML data with xmltodict, the problem often occurs that if only one occurs, a single dictionary is returned instead of a list. This can lead to errors if the code assumes that there is always a list (e.g. when iterating and applying methods such as .update()).

There are two approaches to solving this:

An explicit check to see if the return value is a dictionary, and then converting it to a list. The use of the parameter force_list, e.g. force_list=('Item',), to ensure that the key “Item” is always returned as a list. Since the force_list parameter is only briefly mentioned in the current xmltodict documentation and is not fully documented, I suggest extending the documentation. Specifically, it should explain how force_list is used and the advantages and limitations of this parameter. It would also be helpful to include examples that illustrate the difference between the two approaches.

Thank you very much for your attention and I look forward to your feedback.

ByteEngeneer avatar Feb 19 '25 09:02 ByteEngeneer

This sounds reasonable. Feel free to send a pull request to address this.

martinblech avatar Feb 19 '25 13:02 martinblech

We expanded the README’s API Reference with detailed force_list usage and examples. Closing as ‘docs clarified’.

martinblech avatar Sep 11 '25 22:09 martinblech