
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to generate XML using Python?
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.
example
a = { 'foo': 45, 'bar': { 'baz': "Hello" } } xml = dicttoxml.dicttoxml(a) print(xml)
Output
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.
example
from xml.dom.minidom import parseString a = { 'foo': 45, 'bar': { 'baz': "Hello" } } xml = dicttoxml.dicttoxml(a) dom = parseString(xml) print(dom.toprettyxml())
Output
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>
- Related Questions & Answers
- How to generate XML from Python dictionary?
- How to generate XML documents with namespaces in Python?
- How to generate XML with nested values ?
- How to generate prime numbers using Python?
- How to generate prime twins using Python?
- How to generate JSON output using Python?
- How to generate statistical graphs using Python?
- How to convert XML to Json and Json back to XML using Newtonsoft.json?
- How to serialize Python dictionary to XML?
- How to generate a 24bit hash using Python?
- How to generate pyramid of numbers using Python?
- Fast XML parsing using Expat in Python
- How to generate secure temporary file name using Python?
- How to generate a string of bits using Python?
- How can I serialize python objects to XML?
Advertisements