XML(可扩展标记语言)是一种广泛使用的半结构化数据格式。在处理XML数据时,Python提供了各种第三方库。其中,xmltodict是一个非常流行的解析器,它可以将XML数据转换成Python 字典,以便更方便地处理。
一、安装和使用
xmltodict可以通过pip安装:
pip install xmltodict
安装后,可以import xmltodict
导入。
XML文件可以作为文件读取,使用with open()
方法即可。
import xmltodict
with open('example.xml') as f:
data = xmltodict.parse(f.read())
该解析器可以处理XML文件,甚至是使用HTTP GET请求获得的XML响应。使用requests库做一个简单的例子:
import requests
import xmltodict
url = 'http://www.example.com/example.xml'
r = requests.get(url)
data = xmltodict.parse(r.content)
二、XML转换为Python字典
xmltodict.parse()方法可以将XML文件转换成Python字典。以下是一个简单的XML文件示例:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</catalog>
使用xmltodict.parse()方法将其转换成Python字典:
import xmltodict
xml_str = '''
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</catalog>
'''
data = xmltodict.parse(xml_str)
print(data)
输出结果:
{'catalog': {'book': {'@id': 'bk101', 'author': 'Gambardella, Matthew', 'title': "XML Developer's Guide", 'genre': 'Computer', 'price': '44.95', 'publish_date': '2000-10-01', 'description': 'An in-depth look at creating applications \n with XML.'}}}
可以看到,XML文件的每个元素都转换成了Python字典的键和值,每个元素内的属性也转换成了Python字典的键和值。如果XML文件中有子元素,则会将其转换为Python字典中的字典。注意,每个元素的值都是作为字符串存储的,需要进行类型转换。
三、Python字典转换为XML
xmltodict.unparse()方法可以将Python字典转换成XML文件。以下是一个简单的Python字典示例:
import xmltodict
dictionary = {'catalog': {'book': {'@id': 'bk101', 'author': 'Gambardella, Matthew', 'title': "XML Developer's Guide", 'genre': 'Computer', 'price': 44.95, 'publish_date': '2000-10-01', 'description': 'An in-depth look at creating applications with XML.'}}}
使用xmltodict.unparse()方法将其转换成XML格式:
import xmltodict
dictionary = {'catalog': {'book': {'@id': 'bk101', 'author': 'Gambardella, Matthew', 'title': "XML Developer's Guide", 'genre': 'Computer', 'price': 44.95, 'publish_date': '2000-10-01', 'description': 'An in-depth look at creating applications with XML.'}}}
xml_str = xmltodict.unparse(dictionary)
print(xml_str)
输出结果:
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
可以看到,将Python字典转换成XML格式的过程与将XML文件转换成Python字典的过程正好相反。 注意,数值类型的值不需要额外的引号。在生成的XML文件中,每个元素的值都被排版成了一行。
四、XML文件中的特殊字符和转义字符
XML文件中有一些特殊字符和转义字符,如<
、>
等,将其放在XML文件中时需要对其进行转义,否则会导致解析器无法正确解析。 xmltodict.parse()方法以及xmltodict.unparse()方法可以正确的处理这些特殊字符和转义字符。以下是一个包含特殊字符和转义字符的XML文件示例:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101">
<author>Gambardella & Drukman</author>
<title><![CDATA[XML Developer's Guide]]></title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</catalog>
将其解析成Python字典,并将结果转换成XML格式:
import xmltodict
xml_str = '''
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101">
<author>Gambardella & Drukman</author>
<title><![CDATA[XML Developer's Guide]]></title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</catalog>
'''
data = xmltodict.parse(xml_str)
xml_str = xmltodict.unparse(data)
print(xml_str)
输出结果:
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<book id="bk101">
<author>Gambardella & Drukman</author>
<title><![CDATA[XML Developer's Guide]]></title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
可以看到,解析器可以正确地处理XML文件中的特殊字符和转义字符。将Python字典转换成XML格式时,特殊字符和转义字符也会被正确地转换。
五、结语
xmltodict是一个非常强大的解析器,它可以将XML文件转换成Python字典,并将Python字典转换成XML文件。使用该解析器可以非常便捷地处理XML数据。