Python中的XML处理模块
XML代表“可扩展标记语言”。它主要用于网页中,网页数据具有特定的结构。它具有元素,由开始标签和结束标签定义。标签是一种标记结构,以<开头,以>结尾。开始标签和结束标签之间的字符是元素的内容。元素可以包含其他元素,这些元素称为“子元素”。
示例
以下是我们将在本教程中使用的XML文件示例。
<?xml version="1.0"?> <Tutorials> <Tutorial id="Tu101"> <author>Vicky, Matthew</author> <title>Geo-Spatial Data Analysis</title> <stream>Python</stream> <price>4.95</price> <publish_date>2020-07-01</publish_date> <description>Learn geo Spatial data Analysis using Python.</description> </Tutorial> <Tutorial id="Tu102"> <author>Bolan, Kim</author> <title>Data Structures</title> <stream>Computer Science</stream> <price>12.03</price> <publish_date>2020-1-19</publish_date> <description>Learn Data structures using different programming lanuages.</description> </Tutorial> <Tutorial id="Tu103"> <author>Sora, Everest</author> <title>Analytics using Tensorflow</title> <stream>Data Science</stream> <price>7.11</price> <publish_date>2020-1-19</publish_date> <description>Learn Data analytics using Tensorflow.</description> </Tutorial> </Tutorials>
使用xml.etree.ElementTree读取xml
此模块提供对xml文件根目录的访问,然后我们可以访问内部元素的内容。在下面的示例中,我们使用名为text的属性并获取这些元素的内容。
示例
import xml.etree.ElementTree as ET
xml_tree = ET.parse('E:\TutorialsList.xml')
xml_root = xml_tree.getroot()
# Header
print('Tutorial List :')
for xml_elmt in xml_root:
for inner_elmt in xml_elmt:
print(inner_elmt.text)
输出
运行以上代码将得到以下结果:
Tutorial List : Vicky, Matthew Geo-Spatial Data Analysis Python 4.95 2020-07-01 Learn geo Spatial data Analysis using Python. Bolan, Kim Data Structures Computer Science 12.03 2020-1-19 Learn Data structures using different programming lanuages. Sora, Everest Analytics using Tensorflow Data Science 7.11 2020-1-19 Learn Data analytics using Tensorflow.
获取xml属性
我们可以获取根标签中属性及其值的列表。一旦我们找到属性,它就能帮助我们轻松地浏览XML树。
示例
import xml.etree.ElementTree as ET
xml_tree = ET.parse('E:\TutorialsList.xml')
xml_root = xml_tree.getroot()
# Header
print('Tutorial List :')
for movie in xml_root.iter('Tutorial'):
print(movie.attrib)输出
运行以上代码将得到以下结果:
Tutorial List :
{'id': 'Tu101'}
{'id': 'Tu102'}
{'id': 'Tu103'}过滤结果
我们还可以使用此模块的findall()函数过滤XML树中的结果。在下面的示例中,我们找出价格为12.03的教程的ID。
示例
import xml.etree.ElementTree as ET
xml_tree = ET.parse('E:\TutorialsList.xml')
xml_root = xml_tree.getroot()
# Header
print('Tutorial List :')
for movie in xml_root.findall("./Tutorial/[price ='12.03']"):
print(movie.attrib)输出
运行以上代码将得到以下结果:
Tutorial List :
{'id': 'Tu102'}使用DOM API解析XML
我们使用xml.dom模块创建一个minidom对象。minidom对象提供了一个简单的解析器方法,可以快速地从XML文件创建DOM树。示例短语调用minidom对象的parse(file [,parser])函数,将文件file指定的XML文件解析到DOM树对象中。
示例
from xml.dom.minidom import parse
import xml.dom.minidom
# Open XML document using minidom parser
DOMTree = xml.dom.minidom.parse('E:\TutorialsList.xml')
collection = DOMTree.documentElement
# Get all the movies in the collection
tut_list = collection.getElementsByTagName("Tutorial")
print("*****Tutorials*****")
# Print details of each Tutorial.
for tut in tut_list:
strm = tut.getElementsByTagName('stream')[0]
print("Stream: ",strm.childNodes[0].data)
prc = tut.getElementsByTagName('price')[0]
print("Price: ", prc.childNodes[0].data)
输出
运行以上代码将得到以下结果:
*****Tutorials***** Stream: Python Price: 4.95 Stream: Computer Science Price: 12.03 Stream: Data Science Price: 7.11
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP