dom & sax xml & ado.net

31
DOM & SAX DOM & SAX XML & ADO.NET XML & ADO.NET GVTH: Phạm Anh Phương Email : [email protected]

Upload: yair

Post on 17-Jan-2016

23 views

Category:

Documents


0 download

DESCRIPTION

DOM & SAX XML & ADO.NET. GVTH: Phạm Anh Phương Email : [email protected]. Thời gian. 20 minutes. Tham khảo.  Websites : http://www.codeproject.com/cs/database/ADONET_and_XML.asp http://www.w3.org/XML http://www.xml.org - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: DOM & SAX  XML & ADO.NET

DOM & SAX DOM & SAX XML & ADO.NETXML & ADO.NET

GVTH: Phạm Anh Phương

Email : [email protected]

Page 2: DOM & SAX  XML & ADO.NET

Thời gianThời gian

20 minutes

Page 3: DOM & SAX  XML & ADO.NET

Tham khảoTham khảo

Websites :

http://www.codeproject.com/cs/database/ADONET_and_XML.asp

http://www.w3.org/XML

http://www.xml.org

http://www.informit.com/guides/content.asp?g=dotnet&seqNum=80&rl=1

Thư viện MSDN

Ebooks :

1/ The.Book.of.SAX-The.Simple.API.for.XML.pdf

2/ MS.Press.Applied.XML.Programming.For.Microsoft.Dot.NET.eBook-LiB.chm

Page 4: DOM & SAX  XML & ADO.NET

Mục lụcMục lục

DOM & SAX

XML & ADO.NET

XMLDocument

Page 5: DOM & SAX  XML & ADO.NET

DOMDOM

Page 6: DOM & SAX  XML & ADO.NET

DOM là gì ?DOM là gì ?

The W3C Document Object Model (DOM) is a platform and language-neutral interfaceinterface that allows programs and scripts to dynamically access and update the content, structure, and style of a document

Interface

Page 7: DOM & SAX  XML & ADO.NET

DOM là gì ?DOM là gì ?

Core DOM

XML DOM

HTML DOM

DOM được chia gồm 3 phần riêng biệt :

Là tập đối tượng thao tác với tài liệu có cấu trúc, XML, HTML

Page 8: DOM & SAX  XML & ADO.NET

<< Interface >><< Interface >>DOMDOM

XML ParserXML Parser HTML ParserHTML Parser

(.NET, .JAVA,..) (IE 6, Firefox, ..)

XXX ParserXXX Parser

DOM là gì ?DOM là gì ?

Page 9: DOM & SAX  XML & ADO.NET

DOM xem tài liệu XML như 1 cây có cấu trúc

Tất cả Element, Attribute, Text đều có thể truy xuất (thêm, xóa và sửa)

Element, Attribute, Text được xem là Node

Tài liệu XML là 1 Node Tree

XML DOM là gì ?XML DOM là gì ?

<?xml version="1.0" ><name> <first>Phuong</first> <middle>Anh</middle> <last>Pham</last></name>

middle

Name

first

last

Phuong

Anh

Pham

Page 10: DOM & SAX  XML & ADO.NET

Một cây gồm nhiều node dùng để thể hiện 1 tài liệu XML và sự kết nối giữa chúng- Trong cây, node cao nhất gọi là root- Mỗi node, trừ root ra có chính xác 1 node cha- Một node có nhiều node con- Node lá là node không có node con- Siblings là những node có cùng cấp với node cha

Node TreeNode Tree

Page 11: DOM & SAX  XML & ADO.NET

Để đọc, cập nhật, tạo và thao tác 1 tài liệu XML ta cần 1 XML Parser

Có nhiều XML Parser được hỗ trợ trong hầu hết các ngôn ngữ (JAVA, JavaScript, .NET, ..)

Parser nạp tài liệu XML vào trong bộ nhớ máy tính và được xem dưới dạng cây Node

Sau đó, dữ liệu được thao tác và xử lý thông qua tập hàm XML DOM API

XML DOM ParserXML DOM Parser

Page 12: DOM & SAX  XML & ADO.NET

XML DOM ParserXML DOM Parser

XML DOM API

Page 13: DOM & SAX  XML & ADO.NET

XML DOM APIXML DOM API

Duyệt các nodes :

- parentNode- childNodes- firstChild- lastChild- nextSibling- previousSibling

Root Element :<bookstore>

Element :<book>

Element :<book>

Element :<book>

Element :<book>

parentNode

lastChild

nextSibling

previousSibling

firstChild

Page 14: DOM & SAX  XML & ADO.NET

Thao tác các Node:- getElementsByTagName- getElmentById- getNamedItem- setAttribute- getAttribute- removeChild- removeAttribute- replaceChild- ..

XML DOM APIXML DOM API

Page 15: DOM & SAX  XML & ADO.NET

SAX SAX

Page 16: DOM & SAX  XML & ADO.NET

SAX (Simple API for XML) Performance và Memory không bị ảnh

hưởng cho dù thao tác với tài liệu XML có dung lượng lớn

Event-Driven API

SAXSAX

Page 17: DOM & SAX  XML & ADO.NET

SAXSAX

Page 18: DOM & SAX  XML & ADO.NET

Xử lý dữ liệu ngay khi Parser chưa xử lý tòan bộ tài liệu XML (Khác so với DOM)

Dữ liệu sau khi được parse sẽ gửi đến Client Program thông qua các hàm Notification Method; dữ liệu này sau đó bị xóa

SAXSAX

Page 19: DOM & SAX  XML & ADO.NET

SAX được hỗ trợ trong JAVA nhưng chưa có trong .NET

Khuyết điểm:- Developer phải tạo cấu trúc lưu trữ thông tin

riêng vì dữ liệu gửi về từ parser sẽ tự động mất sau khi đến Notification Method

- SAX không phù hợp đối với các ứng dụng truy xuất ngẫu nhiên và thực hiện chỉnh sửa phức tạp

SAXSAX

Page 20: DOM & SAX  XML & ADO.NET

XMLDocumentXMLDocument

Page 21: DOM & SAX  XML & ADO.NET

Là đối tượng dùng để thao tác với tài liệu XML trong .NET

Namespace : System.Xml

XMLDocumentXMLDocument

Page 22: DOM & SAX  XML & ADO.NET

XMLDocumentXMLDocument

Tên hàm (Thuộc tính) Ý nghĩaLoad Nạp tài liệu XML

Save Ghi tài liệu XML

-InsertBefore, InsertAfter-PrependChild, AppendChild-RemoveChild, ReplaceChild, RemoveAll

Phương thức truy cập node

- NodeChanging, NodeChanged-NodeInserting, NodeInserted-NodeRemoving, NodeRemoved

Xử lý sự kiện thông qua các delegate

-FirstChild, LastChild-ChildNodes, NextSibling, PreviousSibling, ParentNode

Thao tác duyệt node

Page 23: DOM & SAX  XML & ADO.NET

XMLDocumentXMLDocument

Đọc và lưu

Lấy Root Element :

Page 24: DOM & SAX  XML & ADO.NET

XMLDocumentXMLDocument

Xóa Node

Page 25: DOM & SAX  XML & ADO.NET

XMLDocumentXMLDocument

Thêm Node

Page 26: DOM & SAX  XML & ADO.NET

XML & ADO.NETXML & ADO.NET

Page 27: DOM & SAX  XML & ADO.NET

Đối tượng : DataSet Ý nghĩa : Một Database nhưng được lưu trong

memory Thao tác :

- Tạo mối quan hệ giữa các bảng- Thêm, xóa, sửa các record trong bảng

Với XML :- Ghi dữ liệu thành file XML- Đọc dữ liệu từ file XML và nạp lại vào DataSet

XML & ADO.NETXML & ADO.NET

Page 28: DOM & SAX  XML & ADO.NET

Một số phương thức :- GetXml- GetXmlSchema- WriteXml- ReadXml- WriteXmlSchema- ReadXmlSchema

XML & ADO.NETXML & ADO.NET

Page 29: DOM & SAX  XML & ADO.NET

Minh họa :

XML & ADO.NETXML & ADO.NET

Page 30: DOM & SAX  XML & ADO.NET

Hỏi & ĐápHỏi & Đáp

Page 31: DOM & SAX  XML & ADO.NET

Cảm ơnCảm ơn