使用 Python 中的 Expat 快速解析 XML

pythonserver side programmingprogramming更新于 2025/5/16 9:16:17

Python 允许通过其内置模块 expat 读取和处理 XML 数据。它是一个非验证 XML 解析器。它创建一个 XML 解析器对象并将其对象的属性捕获到各种处理程序函数中。在下面的示例中,我们将看到各种处理程序函数如何帮助我们读取 XML 文件以及将属性值作为输出数据提供。生成的数据可用于处理。

示例

import xml.parsers.expat
# 捕获第一个元素
def first_element(tag, attrs):
   print ('first element:', tag, attrs)
# 捕获最后一个元素
def last_element(tag):
   print ('last element:', tag)
# 捕获字符数据
def character_value(value):
   print ('Character value:', repr(value))
parser_expat = xml.parsers.expat.ParserCreate()
parser_expat.StartElementHandler = first_element
parser_expat.EndElementHandler = last_element
parser_expat.CharacterDataHandler = character_value
parser_expat.Parse(""" <?xml version="1.0"?>
<parent student_rollno="15">
<child1 Student_name="Krishna"> Strive for progress, not perfection</child1>
<child2 student_name="vamsi"> There are no shortcuts to any place worth going</child2>
</parent>""", 1)

输出

运行上述代码得到以下结果 −

first element: parent {'student_rollno': '15'}
Character value: '\n'
first element: child1 {'Student_name': 'Krishna'}
Character value: 'Strive for progress, not perfection'
last element: child1
Character value: '\n'
first element: child2 {'student_name': 'vamsi'}
Character value: ' There are no shortcuts to any place worth going'
last element: child2
Character value: '\n'
last element: parent

相关文章


有用资源