How to serialize Python dictionary to XML?

PythonServer Side ProgrammingProgrammingXML

Use dicttoxml package to convert Python dictionary to xml representation.

To start, install dicttoxml package

pip3 install dicttoxml

Create a dictionary object

>>> D1={"name":"Ravi", "age":21, "marks":55}

Now import dicttoxml() function from dicttoxml package and use D1 as argument. The function returns encoded string as xml representation of dictionary

>>>fromdicttoxml import dicttoxml

Obtain decode string by decode() function

The resulting string contains xml version of dictionary

'<?xml version="1.0" encoding="UTF-8" ?><root><name type="str">Ravi</name><age type="int">21</age><marks type="int">55</marks></root>'

You can even store it in an xml file

Published on 09-Feb-2018 00:24:03