xml parser week2
Post on 13-Apr-2017
146 Views
Preview:
TRANSCRIPT
What?
กระบวนการ parse เอกสาร XML เพอจดการแสดง มไดหลายวธ โดยหลกๆทใช กจะมอย
2 อยาง ซงมขอดขอเสยตางๆกนคอ
1. DOM (Document Object Model) เปนวธทประมวลโครงสรางของเอกสาร XML ใหเปนโครงสรางแบบตนไม เพอใหแอพพลเคชนสามารถเขาหาจดตาง ๆ ของโครงสรางตนไมได โดยท DOM จะโหลดไฟล XML ทงไฟลใน Memory ซงกมท งขอดและขอเสย
ขอด คอ ครงแรกสดทมการเรยกใชงานจะชา แตหลงจากนนการเขาถงจดตาง ๆ ของไฟล จะเรวเพราะถกเกบใน memory แลว
ขอเสย คอ ไมเหมาะกบ file XML ทมขนาดใหญ โดยทขอบเขตจะถกจ ากดดวยหนวยความจ าทเรยกใชในขณะนน
2. SAX (Simple API for XML) เปนการท างานแบบ event-based API คอจะรายงานขอมล
ตงแตจดเรมตนและสนสดของ elements ตาง ๆ ไปใหแอพพลเคชนโดยไมตองมการสรางโครงสรางแบบตนไมขนมา ซงจะเขาถงเอกสาร XML ท างานไดงายและไมซบซอน และทส าคญผใชสามารถท า parsing เอกสารทมขนาดใหญกวาปรมาณหนวยความจ าได
สราง XML file
จากขอมลดบ ภาพตวอยางขอมลดบ: รายละเอยดตางๆของภาพยนตร ทงหมด 60 เรอง
สราง XML file
จากขอมลดบ <?xml version="1.0" encoding="UTF-8"?>
<film>
<movie>
<name>Always</name>
<types>
<type>Drama</type>
<type>Romance</type>
</types>
<stars>
<name_actor>So Ji-seob</name_actor>
<name_actor>Han Hyo-joo</name_actor>
</stars>
<director>Song Il-Gon</director>
<date>
<day>2</day>
<month>Feb</month>
<year>2012</year>
</date>
</movie>
</film>
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() คนหาขอมล
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("\t\t"+type_.text)
print ("\tMain character:")
for name_actor in movie.find('stars'):
print("\t\t"+name_actor.text)
print ("\tDirector:")
print ("\t\t"+movie.find('director').text)
print ("\tDate:")
date_ = movie.find('date') print ("\t\t"+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
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 ===============================
Parse XML Python
Tom Hanks: The Da Vinci Code Cloud Atlas Forrest Gump The Terminal ===============================
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
Reference
• What? http://www.goragod.com/knowledge/ • Parse XML Python https://docs.python.org/2/library/xml.etree.elementtree.html https://python3.wannaphong.com/2015/04/python-xml.html
Members
Group1 นาย อภวฒธ วงศโทะ รหสนกศกษา 52-1116-530-2 นางสาว พลลภา เขมรงสฤษฏ รหสนกศกษา 56-010126-2008-1 นางสาว อญธกา หนองบว รหสนกศกษา 56-010126-3028-1 นาย ธรวฒน ผองสกล รหสนกศกษา 56-010126-3015-9 นาย ธนดล เตชะวชรกล รหสนกศกษา 56-010126-3009-4 นาย ภมมฑล ไชยเชดเกยรต รหสนกศกษา 56-010116-2131-8
top related