Home Code Convert Python Dictionary or List of Dictionaries to XML string

Convert Python Dictionary or List of Dictionaries to XML string

You can easily convert a Python Dict or a list of Dicts to a valid XML string. To do so, you need to first install the Python Package dicttoxml. Below is the PIP command install it.

pip install dicttoxmlCode language: Bash (bash)

Once you have installed the package using the above command, use the below function to convert any dictionary or list of dictionaries into an XML string.

from dicttoxml import dicttoxml
from xml.dom.minidom import parseString
def dict2xml(dict_or_list,root=True,custom_root='root',cdata=True,pretty_print=True,attr_type=False):
    xml = dicttoxml(dict_or_list, root=root, custom_root=custom_root, cdata=cdata, attr_type=attr_type)
    if pretty_print:
        dom = parseString(xml)
        return dom.toprettyxml()
    else:
        return xml.decode("utf-8")Code language: Python (python)

Pretty Print XML document

The above function by default will pretty print the resultant XML string, similar to how you pretty print JSON. That will make it more readable for human eyes and visually appealing if you want to further edit the XML document. You can disable pretty printing by setting the pretty_print argument to False.

Wrapping String values in CDATA of XML document

The cdata argument is True by default to wrap any string into CDATA tag in the output XML. You can set it to False, but that will mean that all XML specific characters like <,>, ” etc will be escaped into html entities like “&lt” “&gt”, “&quot” etc.

Complete XML document with a root element or an XML Snippet

By default the function will generate a complete XML document whose root is ‘root’. You can change the default root name to any value like ‘Items’ or ‘Products’ or whatever you want. If you want to generate an XML Snippet ie XML without any Root node so that you can insert that snippet into another XML document, then simply set root=False.

Type attribute for each Element to indicate value type

The attr_type which is False by default, will output additional type information for each element if set to True.

Content Protection by DMCA.com

Download HitXP Mobile App

Get it on Google Play
Gurudevhttps://www.hitxp.com
Gurudev is the developer of Gurunudi AI Platform. This is his official website where he pens his thoughts on a wide range of topics, answers queries, shares resources and tools developed by him.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

FacebookLike
InstagramFollow
PinterestFollow
RSS FeedSubscribe
Sound CloudFollow
TwitterFollow
YoutubeSubscribe

Latest Articles

The Power of Democracy: How Free Societies Foster Innovation in Science and Technology

Democracy allows freedom of speech and thought, boosting creativity and innovation. This leads to scientific breakthroughs and technological advancements.

How does GOD helps us?

Do prayers really help? Do miracles happen when we are in trouble? Does God really help us in times of need? Are prayers enough to save us during bad times? How should we face problems?

Sanskrit poem on Ramayana – read in reverse becomes Mahabharata!

A Sanskrit poem about Ramayana when read in reverse direction becomes a poem on Mahabharata!

The difference between Itihasa and Puranas

Documentation of ancient Indian history and the historical events of Indian civilization in the form of Itihasa and Puranas - Ramayana and Mahabharata.

Latest Music Notations

Jai Shri Ram – Adipurush – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Jai Shri Ram from the 2023 Hindi movie Adipurush in Western and Indian Notations.

Sojugada Soojumallige – Garuda Gamana Vrishabha Vahana – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Sojugada Soojumallige from the 2021 Kannada movie Garuda Gamana Vrishabha Vahana in Western and Indian Notations.

Oo Antava Mava – Pushpa – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Oo Antava Mava from the 2022 Telugu movie Pushpa in Western and Indian Notations.

Kaa Chalige Bangalore (Tiningaa Miningaa Tishaaaa) – Salaga – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Tiningaa Miningaa Tishaaaa (Kaa Chali Ge) from the 2022 Kannada movie Salaga in Western and Indian Notations.
Content Protection by DMCA.com