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