Search code examples
pythonxmlxml-serializationdicttoxml

Converting a dict to XML with attributes


I am using dicttoxml in python for converting dict to XML .

I need to convert the dict to XML attributes.

For example:

dict

[
      {
           "@name":"Ravi",
           "@age":21,
           "college":"Anna University"
       }
]

Output XML

<Student name="Ravi" age=21>
  <college>Anna University</college>
</Student>

code

dicttoxml(dict, custom_root='Student', attr_type=False, root=True)

Actual Output

<Student>
  <key name="name">Ravi</key>
  <key name="age">21</key>
  <college>Anna University</college>
</Student>

Solution

  • This is not supported by dicttoxml as of yet, though the issue has been open from a long time. https://github.com/quandyfactory/dicttoxml/issues/27

    Though if your needs are not that complex, you can try this simple serializer out.

    https://gist.github.com/reimund/5435343/

    found it here :- Serialize Python dictionary to XML