Xem mẫu
- Dùng DOM để display XML thành nhiều tầng trong TreeView
I nternet Explorer 5.0 cho ta Document Object Model (DOM) ActiveX gọi là
MSXML.DLL mà ta có thể dùng trong VB6. Ðầu tiên là Microsoft XML, version 2.0,
tiếp theo đó là Microsoft XML, v2.6 và mới nhất là Microsoft XML, v3.0. Cả ba DLL
nầy đều có trong danh sách các References mà ta có thể include khi dùng IDE Menu
command Project | References.
Khi ta Load một XML file vào DOM, nó tự động parse XML data để build một Tree
gồm nhiều nodes với thứ bậc cha, con bên trong. Dựa theo đó ta có thể display cái DOM
Tree ấy trong một TreeView để có thể hình dung được cấu trúc của XML data.
Trong thí dụ dưới đây, ta Load một XML file tên Library.xml vào DOM. XML file nầy
còn có một Schema file tên LibrarySchema.xml. Khi DOM load XML file, ta có thể dặn
nó kiểm (validate) xem XML data có theo đúng tiêu chuẩn đòi hỏi trong Schema file.
Content của Library.xml như sau, lưu ý hàng thứ 7 nhắc đến LibrarySchema.xml mà
DOM sẽ dùng để validate data trong XML file:
Northmead Local Library
C++ Programming for Beginners
Claude
Schwartz
005.133/C
http://library/online_books/005133C.html
XML Users Journal August 1999
1999-08-01
005.133/C
Titanic
James Cameron
643.11/T
-
The C Programming Language
Brian
Kernighan
Dennis
Ritchie
005.133/C2
http://library/online_books/005133C2.html
Content của LibrarySchema.xml như sau:
-
-
Việc đầu tiên khi chạy program là bạn click nút Load XML and Display in TreeView.
Ðợi một chút xíu, Tree của XML sẽ hiện ra trong TreeView.
Ðồng thời Content của XML file cũng được loaded vào ListBox lstXMLSource và bạn
sẽ thấy nó nếu bạn click Tab XML Source. Dĩ nhiên bạn có thể display bất cứ một XML
file nào nếu bạn để nó vào folder của program và enter Filename của nó vào TextBox
txtXMLFileName trứớc khi click nút Load XML and Display in TreeView.
- Trong program nầy ta dùng Object IXMLDOMNode, thay vì Object
IXMLDOMElement để lần lượt đi qua mọi nodes của XML DOM. Program gọi Sub
AddNode để bỏ các Nodes vào TreeView. Ðặc biệt là AddNode gọi chính nó ở bên trong
Sub AddNode. Kỹ thuật nầy gọi là recursive, mà ta thường lấy dùng trong những cấu
trúc giống như nhánh cây, khi chính một Con lại có nhiều Con khác. Listing của Sub
AddNode như sau:
Private Sub AddNode(ByRef oElem As IXMLDOMNode, Optional ByRef oTreeNode
As Node)
' Add a Node to the TreeView
Dim oNewNode As Node
Dim oNodeList As IXMLDOMNodeList
Dim i As Long
- ' Create the new node
If oTreeNode Is Nothing Then
' Go through here when creating the top level nodes, i.e. childNodes of root node
Set oNewNode = TreeView.Nodes.Add
Else
Set oNewNode = TreeView.Nodes.Add(oTreeNode, tvwChild)
End If
' Expand TreeView node
oNewNode.Expanded = True
' Prepare the Text for the TreeView Node
If oElem.nodeType = NODE_ELEMENT Then
' Element Node type. Use Node name and Attribute values
oNewNode.Text = BuildNodeLabel(oElem)
ElseIf (oElem.nodeType = NODE_TEXT) Then
' Last Node in the branch. Use Text
oNewNode.Text = oElem.Text
ElseIf (oElem.nodeType = NODE_COMMENT) Then
' Comment Node. Display the comment
oNewNode.Text = "Comment:" & oElem.Text
Else
' Display Nodename as default
oNewNode.Text = oElem.nodeName
End If
' process the childNodes which form a NodeList
Set oNodeList = oElem.childNodes
' Iterate through each childNode
For i = 0 To oNodeList.length - 1
' Recursively call AddNode to add more nodes as children of oNewNode,
' treating AddNode just like another Sub
AddNode oNodeList.Item(i), oNewNode
Next
End Sub
Có ba loại Nodes ta xử lý ở đây: NODE_ELEMENT, NODE_TEXT và
NODE_COMMENT. Element Node thì có Node , Attributes và Con. Text Node và
Comment Node thì chỉ có text.
Bạn có thể download chương trình mẫu XMLTreeDOM.zip để chạy thử.
Ðể biết thêm các Properties và Methods của các Classes trong MSXML, từ trong VB6
IDE bạn press F2 để display Object Browser. Khi Object Browser Dialog hiện ra, chọn
MSXML2 từ ComboBox phía trên đang display , kế đó chọn một class,
- thí dụ như IXMLDOMNode từ ListBox bên trái, chi tiết của selected Class sẽ được
displayed trong ListBox bên phải như trong hình dưới đây:
nguon tai.lieu . vn