0
d = {"a":"a1234","b":"b5678","c":"c4554545"}

Tried converting this dictionary d to xml, as below

<?xml version="1.0" encoding="UTF-8" ?>
<test>
  <a>a1234</a>
  <b>b5678</b>
  <c>c4554545</c>
</test>

Code:

from dicttoxml import dicttoxml

xml = dicttoxml(d, custom_root='test', attr_type=False)
# Above 'xml' is of type bytes here

xml = xml.decode("utf-8")   # Converting bytes to string
print(xml) # prints, <?xml version="1.0" encoding="UTF-8" ?><test><a>a1234</a><b>b5678</b><c>c4554545</c></test>

Tried printing above xml output with pretty print, but end up obtaining below (excludes <?xml version)

<test>
<a>a1234</a>
<b>b5678</b>
<c>c4554545</c>
</test>

How to pretty print as below ?

<?xml version="1.0" encoding="UTF-8" ?>
  <test>
    <a>a1234</a>
    <b>b5678</b>
    <c>c4554545</c>
  </test>
Fresher
  • 97
  • 2
  • 7
  • "Tried printing above xml output with pretty print..." Does that mean that you used BeautifulSoup's `prettify()`? (https://www.crummy.com/software/BeautifulSoup/bs4/doc/#pretty-printing) – mzjn Sep 13 '20 at 14:22
  • Does this answer your question? [Pretty printing XML in Python](https://stackoverflow.com/questions/749796/pretty-printing-xml-in-python) – mzjn Sep 15 '20 at 06:06

0 Answers0