使用属性将 dict 转换为 XML [英] Converting a dict to XML with attributes

查看:45
本文介绍了使用属性将 dict 转换为 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 python 中使用 dicttoxml 将 dict 转换为 XML .

我需要将 dict 转换为 XML 属性.

例如:

字典

<预><代码>[{"@name":"拉维","@年龄":21,"学院":"安娜大学"}]

输出 XML

<学院>安娜大学</学院></学生>

代码

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

实际输出

<key name="name">Ravi</key><key name="age">21</key><学院>安娜大学</学院></学生>

解决方案

dicttoxml 目前还不支持这个问题,尽管这个问题已经存在很长时间了.https://github.com/quandyfactory/dicttoxml/issues/27

不过,如果您的需求不是那么复杂,您可以试试这个简单的序列化器.

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

在这里找到:- 将 Python 字典序列化为 XML

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>

解决方案

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

这篇关于使用属性将 dict 转换为 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆