官方文档
pip install dicttoxml
# 方法一 导入库
import dicttoxml
xml = dicttoxml.dicttoxml(some_dict)
# 方法二 导入dicttoxml()函数
form dicttoxml import dicttoxml
xml = dicttoxml(some_dict)
>>> xml_snippet = dicttoxml.dicttoxml(obj, root=False)
>>> print(xml_snippet)
<mylist><item type="str">foo</item><item type="str">bar</item><item type="str">baz</item></mylist><mydict><foo type="str">bar</foo><baz type="int">1</baz></mydict><ok type="bool">true</ok>
>>> xml = dicttoxml.dicttoxml(obj, custom_root='some_custom_root')
>>> print(xml)
<?xml version="1.0" encoding="UTF-8" ?><some_custom_root><mydict><foo>bar</foo><baz>1</baz></mydict><mylist><item>foo</item><item>bar</item><item>baz</item></mylist><ok>true</ok></some_custom_root>
虽然省略,但隐含默认编码信息仍然是:UTF-8
>>> xml = dicttoxml.dicttoxml(xml_declaration=False)
>>> print(xml)
<root><ok type="bool">true</ok><mylist type="list"><item type="str">foo</item><item type="str">bar</item><item type="str">baz</item></mylist><mydict type="dict"><foo type="str">bar</foo><baz type="int">1</baz></mydict></root>
>>> xml = dicttoxml.dicttoxml(obj, attr_type=False)
>>> print(xml)
<?xml version="1.0" encoding="UTF-8" ?><root><mydict><foo>bar</foo><baz>1</baz></mydict><mylist><item>foo</item><item>bar</item><item>baz</item></mylist><ok>true</ok></root>
>>> xml = dicttoxml.dicttoxml(obj, encoding="ISO-8859-1")
>>> xml = dicttoxml.dicttoxml(obj, include_encoding=False)
为每个元素提供一个唯一的id属性,默认False
>>> xml_with_ids = dicttoxml.dicttoxml(obj, ids=True)
>>> print(parseString(xml_with_ids).toprettyxml())
<?xml version="1.0" ?>
<root>
<mylist id="root_160980" type="list">
<item id="mylist_609405_1" type="str">foo</item>
<item id="mylist_609405_2" type="str">bar</item>
<item id="mylist_609405_3" type="str">baz</item>
</mylist>
<mydict id="root_140407" type="dict">
<foo id="mydict_260437" type="str">bar</foo>
<baz id="mydict_111194" type="int">1</baz>
</mydict>
<ok id="root_612831" type="bool">true</ok>
</root>
dict
基类派生的类似dict的对象,并将其视为dict。例如:>>> import collections
>>> dictlike = collections.OrderedDict({'foo': 1, 'bar': 2, 'baz': 3})
>>> xml = dicttoxml.dicttoxml(dictlike)
>>> print(xml)
3 1 2
>>> myiter = range(1,11)
>>> xml = dicttoxml.dicttoxml(myiter)
>>> print(xml)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
不希望列表中项目元素的名称为item,可自定义名称,默认 item
>>> import dicttoxml
>>> obj = {u'mylist': [u'foo', u'bar', u'baz'], u'mydict': {u'foo': u'bar', u'baz': 1}, u'ok': True}
>>> my_item_func = lambda x: 'list_item'
>>> xml = dicttoxml.dicttoxml(obj, item_func=my_item_func)
>>> print(xml)
<?xml version="1.0" encoding="UTF-8" ?><root><mydict type="dict"><foo type="str">bar</foo><baz type="int">1</baz></mydict><mylist type="list"><list_item type="str">foo</list_item><list_item type="str">bar</list_item><list_item type="str">baz</list_item></mylist><ok type="bool">True</ok></root>
cdata
参数设置为True
在CDATA中包装值。>>> import dicttoxml
>>> obj = {u'mylist': [u'foo', u'bar', u'baz'], u'mydict': {u'foo': u'bar', u'baz': 1}, u'ok': True}
>>> xml = dicttoxml.dicttoxml(obj, cdata=True)
>>> print(xml)
<?xml version="1.0" encoding="UTF-8" ?><root><mydict type="dict"><foo type="str"><![CDATA[bar]]></foo><baz type="int"><![CDATA[1]]></baz></mydict><mylist type="list"><item type="str"><![CDATA[foo]]></item><item type="str"><![CDATA[bar]]></item><item type="str"><![CDATA[baz]]></item></mylist><ok type="bool"><![CDATA[True]]></ok></root>
>>> xml = dicttoxml.dicttoxml(obj)
>>> type(xml).__name__
'bytes'
>>> xml = dicttoxml.dicttoxml(obj, return_bytes=False)
>>> type(xml).__name__
'str'
>>> from xml.dom.minidom import parseString
>>> dom = parseString(xml)
>>> print(dom.toprettyxml())
- foo
- bar
- baz
bar
1
true
dicttoxml.log
>>> import dicttoxml
>>> dicttoxml.set_debug(debug=True)
Debug mode is on. Events are logged at: dicttoxml.log
>>> xml = dicttoxml.dicttoxml(some_dict)
>>> dicttoxml.set_debug(debug=True, filename='/path/to/some_other_filename.log')
Debug mode is on. Events are logged at: some_other_filename.log
False
的参数调用set_debug
>>> dicttoxml.set_debug(debug=False)