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 dicttoxml
Code 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 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.

Online Education should be interactive and innovative – Fun to Learn

For online education to succeed for smaller classses - it should be as interesting as cartoons. Online education cant succeed unless it is more interesting, interactive and innovative than offline schooling.

The Complete List of Dhatus – Sanskrit Root Words and their English meaning

A complete list of all dhatus (root words or verb roots) in Sanskrit dictionary and their meanings with corresponding IPA transliteration.

Sanskrit Lesson 4 – Word Creation Magic using Dhatus

Learn how easy it is to create new words in Sanskrit using root words called Dhatus. This simple process of Sanskrit grammar powers the entire language.

Latest Music Notations

Kempadavo Ella Kempadavo – Ellindalo Bandavaru – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Kempadavo Ella Kempadavo from the 1980 Kannada movie Ellindalo Bandavaru in Western and Indian Notations.

Bella Ciao – Money Heist (La casa de papel) – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Bella Ciao from the 2018 Spanish Web Series Money Heist (La casa de papel) in Western and Indian Notations.

Haule Haule – Rab Ne Banadi Jodi – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Haule Haule from the 2008 Hindi movie Rab Ne Banadi Jodi in Western and Indian Notations.

Gori Tera Gaon Bada Pyara – Chitchor – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Gori Tera Gaon Bada Pyara from the 1976 Hindi movie Chitchor in Western and Indian Notations.
Content Protection by DMCA.com