XML ย่อมาจาก "Extensible Markup Language" ใช้หลักในเว็บเพจ โดยข้อมูลมีโครงสร้างเฉพาะ มี Elements ที่กำหนดโดยแท็กเริ่มต้นและแท็กปิด แท็กคือโครงสร้างมาร์กอัปที่เริ่มด้วย < และจบด้วย > ตัวอักษรระหว่างแท็กเริ่มและแท็กปิด คือเนื้อหาของ Element Elements สามารถมี Elements อื่นๆ ภายในได้ เรียกว่า "child elements"
ตัวอย่าง
ด้านล่างคือตัวอย่างไฟล์ 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 ด้วย xml.etree.ElementTree
โมดูลนี้ให้การเข้าถึงรากของไฟล์ XML และเราสามารถเข้าถึงเนื้อหาของ Elements ภายในได้ ในตัวอย่างด้านล่าง เราใช้แอตทริบิวต์ text เพื่อดึงเนื้อหาของ Elements นั้นๆ
ตัวอย่าง
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 tree ได้ง่ายขึ้น
ตัวอย่าง
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'}
การกรองผลลัพธ์
เรายังสามารถกรองผลลัพธ์จาก XML tree ได้โดยใช้ฟังก์ชัน findall() ของโมดูลนี้ ในตัวอย่างด้านล่าง เราหา id ของ tutorial ที่มีราคา 12.03
ตัวอย่าง
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'}
การแยกวิเคราะห์ XML ด้วย DOM APIs
เราสร้างออบเจ็กต์ minidom โดยใช้โมดูล xml.dom ออบเจ็กต์ minidom ให้เมธอด parser แบบง่ายที่สร้าง DOM tree จากไฟล์ XML ได้อย่างรวดเร็ว ฟังก์ชันตัวอย่างเรียกใช้ parse( file [,parser] ) ของออบเจ็กต์ minidom เพื่อแยกวิเคราะห์ไฟล์ XML ที่ระบุโดย file ให้เป็นออบเจ็กต์ DOM tree
ตัวอย่าง
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