Title | Load a ListView control from an XML file in Visual Basic 6 |
Description | This example shows how to load a ListView control from an XML file in Visual Basic 6. |
Keywords | ListView, load, XML, Visual Basic 6 |
Categories | Controls, Internet, Files and Directories, Software Engineering |
|
|
When the form loads, the program builds a DOMDocument object holding the XML data. It uses function GetAttributesAndNames to get a list of the attributes and element names in the first Employee node's subtree. It splits the list of names apart and uses them to set the ListView's column headers.
Next the program loops through the file's Employee elements. It calls function GetAttributeAndNameValues to get the attribute and name values corresponding to the column names and adds them to the ListView.
|
|
Private Sub Form_Load()
Dim file_name As String
Dim xml_doc As DOMDocument
Dim root_node As IXMLDOMElement
Dim employee_node As IXMLDOMElement
Dim txt As String
Dim values() As String
Dim i As Integer
Dim lvw_item As ListItem
' Compose the XML file's name.
file_name = App.Path
If Right$(file_name, 1) <> "\" Then file_name = _
file_name & "\"
file_name = file_name & "Employees.xml"
' Read the XML file.
Set xml_doc = New DOMDocument
xml_doc.Load file_name
Set root_node = xml_doc.documentElement
' Use the first Employee node to make the column
' headers.
Set employee_node = root_node.firstChild
txt = GetAttributesAndNames(employee_node)
values = Split(txt, "|")
For i = 0 To UBound(values)
lvwEmployees.ColumnHeaders.Add Text:=values(i)
Next i
' Read the Employee values.
For Each employee_node In root_node.childNodes
txt = GetAttributeAndNameValues(employee_node)
values = Split(txt, "|")
If UBound(values) > 0 Then
Set lvw_item = _
lvwEmployees.ListItems.Add(Text:=values(0))
For i = 1 To UBound(values)
lvw_item.ListSubItems.Add Text:=values(i)
Next i
End If
Next employee_node
End Sub
|
|
Function GetAttributesAndNames returns the attributes and element names for a node's subtree. It first loops through the node's attributes adding their names to a string. It then loops through the node's child nodes.
If the node contains a single text node, then the child is a leaf-level element so the program adds its name to the result.
If the does not contain a single text node, then the program loops through its children calling function GetAttributesAndNames recursively and adding the results to this call's result.
|
|
' Return a string giving the element names and attributes
' for this node's subtree separated by | characters.
Private Function GetAttributesAndNames(ByVal xml_node As _
IXMLDOMElement) As String
Dim result As String
Dim i As Integer
Dim child As IXMLDOMNode
' Get this element's attributes.
result = ""
For i = 0 To xml_node.Attributes.length - 1
result = result & "|" & _
xml_node.nodeName & " " & _
xml_node.Attributes(i).nodeName
Next i
' If the element has a single text child,
' add the element's name.
If (xml_node.childNodes.length = 1) And _
(xml_node.firstChild.nodeType = NODE_TEXT) _
Then
' It's a text node. Use its name.
result = result & "|" & xml_node.nodeName
Else
' Get the sub-tree's names and attributes.
For Each child In xml_node.childNodes
If TypeOf child Is IXMLDOMElement Then
result = result & "|" & _
GetAttributesAndNames(child)
End If
Next child
End If
If Len(result) > 0 Then result = Mid$(result, 2)
GetAttributesAndNames = result
End Function
|
|
Function GetAttributeAndNameValues works much as function GetAttributesAndNames does except it returns values for attributes and text elements rather than their names.
|
|
' Return a string giving the values of element names and
' attributes
' for this node's subtree separated by | characters.
Private Function GetAttributeAndNameValues(ByVal xml_node _
As IXMLDOMElement) As String
Dim result As String
Dim i As Integer
Dim child As IXMLDOMNode
' Get this element's attribute values.
result = ""
For i = 0 To xml_node.Attributes.length - 1
result = result & "|" & _
xml_node.Attributes(i).Text
Next i
' If the element has a single text child,
' add the text.
If (xml_node.childNodes.length = 1) And _
(xml_node.firstChild.nodeType = NODE_TEXT) _
Then
' It's a text node. Use its name.
result = result & "|" & xml_node.firstChild.Text
Else
' Get the sub-tree's name and attribute values.
For Each child In xml_node.childNodes
If TypeOf child Is IXMLDOMElement Then
result = result & "|" & _
GetAttributeAndNameValues(child)
End If
Next child
End If
If Len(result) > 0 Then result = Mid$(result, 2)
GetAttributeAndNameValues = result
End Function
|
|
|
|