Programming Tip -How to generate XML from Python dictionary in python?
Programming Tip -How to generate XML from Python dictionary in python?
Today i am going to show Programming Tip - How to generate XML from Python dictionary in python with simple code. I hope you will understand this.
If you have any query you can ask.
i hope you will like it.
To generate XML from a python dictionary, you need to install the dicttoxml package. You can install it using:
$ pip install dicttoxml
Once installed, you can use the dicttoxml method to create the xml. For example,
a = {
'foo': 45,
'bar': {
'baz': "Hello"
}
}
xml = dicttoxml.dicttoxml(a)
print(xml)
This will give the output:
b'<?xml version="1.0" encoding="UTF-8" ?><root><foo type="int">45</foo><bar type="dict"><baz type="str">Hello</baz></bar></root>'
You can also prettyprint this output using the toprettyxml method. For example,
from xml.dom.minidom import parseString
a = {
'foo': 45,
'bar': {
'baz': "Hello"
}
}
xml = dicttoxml.dicttoxml(a)
dom = parseString(xml)
print(dom.toprettyxml())
This will give the output:
<?xml version = "1.0" ?>
<root>
<foo type = "int">45</foo>
<bar type = "dict">
<baz type="str">Hello</baz>
</bar>
</root>
Follow me at : https://steemit.com/@ahmadhassan
=========================================================
Thanks for reading and always welcome your suggestions :) =========================================================
This post has received a 1.19 % upvote from @booster thanks to: @ahmadhassan.
Congratulations @ahmadhassan! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
You published a post every day of the week
Click on any badge to view your own Board of Honor on SteemitBoard.
To support your work, I also upvoted your post!
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP