Introduction to python dicttoxml module

Introduction to dicttoxml module

Official documentation

Install
pip install dicttoxml
Basic usage
# 方法一 导入库
import dicttoxml
xml = dicttoxml.dicttoxml(some_dict)
# 方法二 导入dicttoxml()函数
form dicttoxml import dicttoxml
xml = dicttoxml(some_dict)
Introduction to dicttoxml attributes
root = False
Create an xml fragment instead of a complete xml document, default True
>>> 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>
custom_root="root element name" (starting from version 1.5)
Custom root, default root
>>> 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>
xml_declaration = False (starting from version 1.7.15)
Omit xml declaration (<?xml version="1.0" encoding="UTF-8" ?>)

Although omitted, the implicit default encoding information is still: 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>
attr_type = False (from version 1.4)
Disable type attribute, default True
>>> 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>
encoding="encoding format" (starting from version 1.7.6)
Change the XML encoding attribute. If not changed, the default is: UTF-8
>>> xml = dicttoxml.dicttoxml(obj, encoding="ISO-8859-1")
include_encoding=False (starting from version 1.7.6)
Completely suppresses encoding attributes, does not include encoding attributes, and has no implicit default encoding attributes. Defaults to True.
>>> xml = dicttoxml.dicttoxml(obj, include_encoding=False)
ids=True (version 1.1)

Provide a unique id attribute for each element, default 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>
Starting with version 1.3, dicttoxml accepts dictdict-like objects derived from a base class and treats them as dicts. For example:
>>> import collections
>>> dictlike = collections.OrderedDict({'foo': 1, 'bar': 2, 'baz': 3})
>>> xml = dicttoxml.dicttoxml(dictlike)
>>> print(xml)
<?xml version="1.0" encoding="UTF-8" ?><root><baz type="int">3</baz><foo type="int">1</foo><bar type="int">2</bar></root>
Also starting with version 1.3, dicttoxml accepts iterable objects and treats them as lists. For example:
>>> myiter = range(1,11)
>>> xml = dicttoxml.dicttoxml(myiter)
>>> print(xml)
<?xml version="1.0" encoding="UTF-8" ?><root><item type="int">1</item><item type="int">2</item><item type="int">3</item><item type="int">4</item><item type="int">5</item><item type="int">6</item><item type="int">7</item><item type="int">8</item><item type="int">9</item><item type="int">10</item></root>
item_func=my_item_func (starting from version 1.7)

If you do not want the name of the item element in the list to be item, you can customize the name. The default is 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 (starting from version 1.7.1)
cdataParameters are set to Truewrap values ​​in 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>
return_bytes=False (starting from version 1.7.14)
True: returns bytes object (default), False: returns str object
>>> xml = dicttoxml.dicttoxml(obj)
>>> type(xml).__name__
'bytes'
>>> xml = dicttoxml.dicttoxml(obj, return_bytes=False)
>>> type(xml).__name__
'str'
expand:
Formatted printing, combined with xml.dom.minidom module to achieve formatted printing
>>> from xml.dom.minidom import parseString
>>> dom = parseString(xml)
>>> print(dom.toprettyxml())
<?xml version="1.0" ?>
<root>
    <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>
    <ok type="bool">true</ok>
</root>
debug
Enable debugging information using the set_debug method
By default, debugging information is logged todicttoxml.log
>>> import dicttoxml
>>> dicttoxml.set_debug(debug=True)
Debug mode is on. Events are logged at: dicttoxml.log
>>> xml = dicttoxml.dicttoxml(some_dict)
Change the debug information recording path
>>> dicttoxml.set_debug(debug=True, filename='/path/to/some_other_filename.log')
Debug mode is on. Events are logged at: some_other_filename.log
To turn off debug mode, just Falsecall with the argumentsset_debug
>>> dicttoxml.set_debug(debug=False)

Guess you like

Origin blog.csdn.net/qq_39962271/article/details/133310163