python dict转xml

来源:互联网 发布:阿里云 搭建vpn 编辑:程序博客网 时间:2024/06/07 06:06

原文链接: python dict转xml

代码

import dicttoxmlfrom xml.dom.minidom import parseStringd = {'k':[{'k1':'v1'}, {'k2':'v2', 'k3':'v3'}]}xml = dicttoxml.dicttoxml(d, attr_type=False, root=False)print(xml)dom = parseString(xml)xml = dom.toprettyxml()print(xml)

输出

b'<k><item><k1>v1</k1></item><item><k3>v3</k3><k2>v2</k2></item></k>'<?xml version="1.0" ?><k>    <item>        <k1>v1</k1>    </item>    <item>        <k3>v3</k3>        <k2>v2</k2>    </item></k>

https://github.com/quandyfactory/dicttoxml
attr_type, Each element includes an optional type attribute with the data type.
xml.dom.minidom.parseString(xml).toprettyxml(), return a pretty-printed version of the document.

0 0
原创粉丝点击