我在python中使用dict to XML将dict转换为XML。
我需要将dict转换为XML属性。
例如:
听写
[
{
"@name":"Ravi",
"@age":21,
"college":"Anna University"
}
]
输出XML
<Student name="Ravi" age=21>
<college>Anna University</college>
</Student>
代码
dicttoxml(dict, custom_root='Student', attr_type=False, root=True)
实际产量
<Student>
<key name="name">Ravi</key>
<key name="age">21</key>
<college>Anna University</college>
</Student>
我可能建议declxml(完全公开:我写的)。使用declxml,可以创建一个名为processor的对象,该对象声明性地定义XML的结构。您可以使用处理器来解析和序列化XML数据。declxml处理字典、对象和namedtuples之间的序列化。它处理元素的属性和数组并执行基本验证。
产生所需的输出
到目前为止,dicttoxml还不支持这一点,尽管这个问题已经存在很长一段时间了。 https://github.com/quandyfactory/dicttoxml/issues/27
不过,如果您的需求不是那么复杂,可以尝试使用这个简单的序列化程序。
https://gist.github.com/reimund/5435343/
在这里找到的:-Serialize Python dictionary to XML
我有一个相似的要求,将XML转换为dict,反之亦然。我使用了一个名为xmltodict的库。这个lib允许您使用属性从dict转换为xml。
听写
]
代码
相关问题 更多 >
编程相关推荐