如何使用Python生成XML?
要从python字典生成XML,您需要安装dicttoxml软件包。您可以使用安装它-
$ pip install dicttoxml
安装之后,您可以使用dicttoxml方法创建xml。
示例
a = {
'foo': 45,
'bar': {
'baz': "Hello"
}
}
xml = dicttoxml.dicttoxml(a)
print(xml)输出结果
这将给出输出-
b'<?xml version="1.0" encoding="UTF-8" ?><root><foo type="int">45</foo><bar type="dict"><baz type="str">Hello</baz></bar></root>'
您也可以使用toprettyxml方法将该输出漂亮打印。
示例
from xml.dom.minidom import parseString
a = {
'foo': 45,
'bar': {
'baz': "Hello"
}
}
xml = dicttoxml.dicttoxml(a)
dom = parseString(xml)
print(dom.toprettyxml())输出结果
这将给出输出-
<?xml version = "1.0" ?>
<root>
<foo type = "int">45</foo>
<bar type = "dict">
<baz type = "str">Hello</baz>
</bar>
</root>热门推荐
10 圣诞祝福语简短小学
11 祖国七十华诞简短祝福语
12 老师送的祝福语简短
13 生日祝福语大全女生简短
14 祝女性生日祝福语简短
15 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短