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
廣告