Thời gianThời gian
20 minutes
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
Mục lụcMục lục
DOM & SAX
XML & ADO.NET
XMLDocument
DOMDOM
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
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
<< Interface >><< Interface >>DOMDOM
XML ParserXML Parser HTML ParserHTML Parser
(.NET, .JAVA,..) (IE 6, Firefox, ..)
XXX ParserXXX Parser
DOM là gì ?DOM là gì ?
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
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
Để đọ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
XML DOM ParserXML DOM Parser
XML DOM API
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
Thao tác các Node:- getElementsByTagName- getElmentById- getNamedItem- setAttribute- getAttribute- removeChild- removeAttribute- replaceChild- ..
XML DOM APIXML DOM API
SAX SAX
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
SAXSAX
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
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
XMLDocumentXMLDocument
Là đối tượng dùng để thao tác với tài liệu XML trong .NET
Namespace : System.Xml
XMLDocumentXMLDocument
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
XMLDocumentXMLDocument
Đọc và lưu
Lấy Root Element :
XMLDocumentXMLDocument
Xóa Node
XMLDocumentXMLDocument
Thêm Node
XML & ADO.NETXML & 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
Một số phương thức :- GetXml- GetXmlSchema- WriteXml- ReadXml- WriteXmlSchema- ReadXmlSchema
XML & ADO.NETXML & ADO.NET
Minh họa :
XML & ADO.NETXML & ADO.NET
Hỏi & ĐápHỏi & Đáp
Cảm ơnCảm ơn