Programming Tip -How to generate XML from Python dictionary in python?

in #utopian-io6 years ago

Programming Tip -How to generate XML from Python dictionary in python?

EBOOK_PYTHON_no-name.png


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 :) =========================================================

Sort:  

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

Upvote this notification to help all Steemit users. Learn why here!

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63548.34
ETH 2646.78
USDT 1.00
SBD 2.74