Each group presents about XML
Homework:
To try and use XML parser
G1 Python
G2 Lua
G3 Java
G4 Ruby
G5 Javascript
G6 C#
G1-G3 create XML data about books (10 for each member)
G4-G6 create XML data about movies (10 for each member)
5. Parse XML Python
หลังจากสร้างไฟล์ XML แล้ว ต่อมาจะไป Parse XML ด้วย Python โดยใช้ไลบรารี
xml.etree.ElementTree ในการ Parse XM โดยมีคาสั่งที่ใช้งานบ่อย ๆ ดังนี้
• parse(source) ดึงไฟล์ source เข้ามาเพื่อทาการ parse
• getroot() คืนค่า root element
• tag ดึง Element ออกมา
• attrib ดึง Attribute ออกมา
• text แสดงข้อมูลที่อยู่ใน Element
• find() , findall() ค้นหาข้อมูล
6. import xml.etree.ElementTree as ET
tree = ET.parse('MovieAll_SPN.xml')
root = tree.getroot()
for movie in root.findall('movie'):
print (movie.find('name').text)
print ("tType:")
for type_ in movie.find('types'):
print("tt"+type_.text)
print ("tMain character:")
for name_actor in movie.find('stars'):
print("tt"+name_actor.text)
print ("tDirector:")
print ("tt"+movie.find('director').text)
print ("tDate:")
date_ = movie.find('date')
print ("tt"+date_.find('day').text+"-"+date_.find('month').text+"-
"+date_.find('year').text)
print("===============================")
name_serch = "Tom Hanks"
print ("Tom Hankss:")
for movie in root.findall('movie'):
for name_actor in movie.find('stars'):
if(name_serch == name_actor.text):
print ("t"+movie.find('name').text)
print("===============================")
types_serch = "Action"
print ("Action movie:")
for movie in root.findall('movie'):
for type_ in movie.find('types'):
if(types_serch == type_.text):
print ("t"+movie.find('name').text)
Parse XML Python
ดูข้อมูลทั้งหมด
ดูข้อมูลหาพยนต์ที่มี Tom Hanks แสดง
ดูข้อมูลหนังประเภท Action
7. Parse XML Python
Type:
Drama
Romance
Main character:
So Ji-seob
Han Hyo-joo
Director:
Song Il-Gon
Date:
2-Feb-2012
===============================
Letters To Juliet
Type:
Comedy
Drama
Romance
Main character:
Christopher Egan
Amanda Seyfried
Director:
Gary Winick
Date:
4-May-2010
===============================
8. Parse XML Python
Tom Hanks:
The Da Vinci Code
Cloud Atlas
Forrest Gump
The Terminal
===============================
9. Parse XML Python
Action movie:
The Last Airbender
Captain America: Civil War
Lucy
Kingsman: The Secret Service
Sky High
Suicide Squad
Justice League
Resident Evil: The Final Chapter
Rogue One: A Star Wars Story
Star Trek Beyond
Batman v Superman: Dawn of Justice
Ghostbusters
Fast 8
The Avenger
The Avenger 2 : Age of Altron
Ironman
Ironman 2
Ironman 3
The Incredible Hulk
Thor
Thor The Dark World
Ant Man
Captain America: The Winter Soldier
In the Heart of the Sea
Batman Begins
The Dark Knight
The Dark Knight Rises
The Matrix I
The Matrix II
The Matrix III