SlideShare a Scribd company logo
1 of 12
Download to read offline
“หนึ่งหน่วยกลุ่ม” (Group 1)
Selected Topics in Computer Engineering II
XMLparser
Python
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("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
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
Xml parser week2

More Related Content

More from Aey Unthika

Assignmet1:Start by finding web services using SOAP and WSDL on the Internet/...
Assignmet1:Start by finding web services using SOAP and WSDL on the Internet/...Assignmet1:Start by finding web services using SOAP and WSDL on the Internet/...
Assignmet1:Start by finding web services using SOAP and WSDL on the Internet/...
Aey Unthika
 

More from Aey Unthika (8)

Assignmet 2 selectedtopic Topic in Computer Engineer
Assignmet 2 selectedtopic Topic in Computer EngineerAssignmet 2 selectedtopic Topic in Computer Engineer
Assignmet 2 selectedtopic Topic in Computer Engineer
 
Assignmet1 selectedtopic Topic in Computer Engineer
Assignmet1 selectedtopic Topic in Computer EngineerAssignmet1 selectedtopic Topic in Computer Engineer
Assignmet1 selectedtopic Topic in Computer Engineer
 
Assignmet1:Start by finding web services using SOAP and WSDL on the Internet/...
Assignmet1:Start by finding web services using SOAP and WSDL on the Internet/...Assignmet1:Start by finding web services using SOAP and WSDL on the Internet/...
Assignmet1:Start by finding web services using SOAP and WSDL on the Internet/...
 
To try and create DTD and XML Schema
To try and create DTD and XML SchemaTo try and create DTD and XML Schema
To try and create DTD and XML Schema
 
Try PostgreSQL on linux
Try PostgreSQL on linuxTry PostgreSQL on linux
Try PostgreSQL on linux
 
Example Database normal form
Example Database normal formExample Database normal form
Example Database normal form
 
Assignment 2 of Database (Database Security)
Assignment 2 of Database (Database Security)Assignment 2 of Database (Database Security)
Assignment 2 of Database (Database Security)
 
Assignment 1 of Database (MySQL & Sqlite3)
Assignment 1 of Database (MySQL & Sqlite3) Assignment 1 of Database (MySQL & Sqlite3)
Assignment 1 of Database (MySQL & Sqlite3)
 

Xml parser week2

  • 1. “หนึ่งหน่วยกลุ่ม” (Group 1) Selected Topics in Computer Engineering II XMLparser Python
  • 2. 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 เอกสารที่มีขนาดใหญ่กว่าปริมาณหน่วยความจาได้
  • 3. สร้าง XML file จากข้อมูลดิบภาพตัวอย่างข้อมูลดิบ: รายละเอียดต่างๆของภาพยนตร์ ทั้งหมด 60 เรื่อง
  • 4. สร้าง 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>
  • 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
  • 10. 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
  • 11. 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