学习笔记 – 40 – xml文档与字典之间的互相转换

1. 如何将一个字典转换为xml文档,并将该xml文档保存成文本文件

'''
dicttoxml
pip install dicttixml

'''
import dicttoxml
from xml.dom.minidom import parseString

d = [20, 'names', {'name': 'Bill', 'age': '30', 'salary': 2000},
                  {'name': 'Mike', 'age': '20', 'salary': 3000},
                  {'name': 'John', 'age': '40', 'salary': 4000}]

bxml = dicttoxml.dicttoxml(d, custom_root='persons')
xml = bxml.decode('utf-8')
print(xml)

dom = parseString(xml)
prettyxml = dom.toprettyxml(indent='    ')
print(prettyxml)

f = open('persons1.xml', 'w', encoding='utf-8')
f.write(prettyxml)
f.close()

<?xml version="1.0" encoding="UTF-8" ?><persons><item type="int">20</item><item type="str">names</item><item type="dict"><name type="str">Bill</name><age type="str">30</age><salary type="int">2000</salary></item><item type="dict"><name type="str">Mike</name><age type="str">20</age><salary type="int">3000</salary></item><item type="dict"><name type="str">John</name><age type="str">40</age><salary type="int">4000</salary></item></persons>

<?xml version="1.0" ?>

<persons>

    <item type="int">20</item>

    <item type="str">names</item>

    <item type="dict">

        <name type="str">Bill</name>

        <age type="str">30</age>

        <salary type="int">2000</salary>

    </item>

    <item type="dict">

        <name type="str">Mike</name>

        <age type="str">20</age>

        <salary type="int">3000</salary>

    </item>

    <item type="dict">

        <name type="str">John</name>

        <age type="str">40</age>

        <salary type="int">4000</salary>

    </item>

</persons>

<persons>
	<item type="int">20</item>
	<item type="str">names</item>
	<item type="dict">
		<name type="str">Bill</name>
		<age type="str">30</age>
		<salary type="int">2000</salary>
	</item>
	<item type="dict">
		<name type="str">Mike</name>
		<age type="str">20</age>
		<salary type="int">3000</salary>
	</item>
	<item type="dict">
		<name type="str">John</name>
		<age type="str">40</age>
		<salary type="int">4000</salary>
	</item>
</persons>

2. 如何读取xml文档的内容,并将其转换为字典

'''
xmltodict
pip install xmltodict
'''

import xmltodict

f = open('products.xml', 'rt', encoding='utf-8')
xml = f.read()
import pprint
d = xmltodict.parse(xml)
print(d)

pp = pprint.PrettyPrinter(indent=4)
pp.pprint(d)
print(type(d))

OrderedDict([('root', OrderedDict([('products', OrderedDict([('product', [OrderedDict([('@uuid', '1234'), ('id', '10000'), ('name', 'iphone9'), ('price', '9999')]), OrderedDict([('@uuid', '4321'), ('id', '20000'), ('name', '特斯拉'), ('price', '800000')]), OrderedDict([('@uuid', '5678'), ('id', '30000'), ('name', 'Mac Pro'), ('price', '40000')])])]))]))])

OrderedDict([   (   'root',

                    OrderedDict([   (   'products',

                                        OrderedDict([   (   'product',

                                                            [   OrderedDict([   (   '@uuid',

                                                                                    '1234'),

                                                                                (   'id',

                                                                                    '10000'),

                                                                                (   'name',

                                                                                    'iphone9'),

                                                                                (   'price',

                                                                                    '9999')]),

                                                                OrderedDict([   (   '@uuid',

                                                                                    '4321'),

                                                                                (   'id',

                                                                                    '20000'),

                                                                                (   'name',

                                                                                    '特斯拉'),

                                                                                (   'price',

                                                                                    '800000')]),

                                                                OrderedDict([   (   '@uuid',

                                                                                    '5678'),

                                                                                (   'id',

                                                                                    '30000'),

                                                                                (   'name',

                                                                                    'Mac '

                                                                                    'Pro'),

                                                                                (   'price',

                                                                                    '40000')])])]))]))])

<class 'collections.OrderedDict'>

正文完