Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
03/26/2000
Times viewed :
2389
Loading a Microsoft TreeView from an XML DOM
document
Load the DOM document into a DOM object called objPeopleRoot. Create a
Element object called objPersonElement (of the type IXMLDOMElement).
Then use this code to iterate through the elements in the DOM object:
For Each objPersonElement In objPeopleRoot.childNodes
populateTreeWithChildren objPersonElement
Next
As you can see, the code above calls a sub routine called
populateTreeWithChildren, which is listed below. The code assumes you have a MS
VB6 Treeview called tvwPeople on the form.
Private Sub populateTreeWithChildren(objDOMNode As IXMLDOMElement)
'*****************************************************
' Purpose: For each parent node created on the TreeView,
' drill down into the DOM Element that has
' been passed in and populate the TreeView with
' these the DOMElement childNodes.
' Inputs:
' objDOMNode: the current child node from the docElement
' property of the DOMDocument
'*****************************************************
Dim objNameNode As IXMLDOMNode
Dim objAttributes As IXMLDOMNamedNodeMap
Dim objAttributeNode As IXMLDOMNode
Dim intIndex As Integer
Dim tvwElement As Node
Dim tvwChildElement As Node
'we're adding the elements main child as the name of this
'section - which we have chosen as the "NAME" element.
'Use the method selectSingleNode to return the first node
'that it finds with the nodeName of "NAME"
Set objNameNode = objDOMNode.selectSingleNode("NAME")
'Add the "NAME" element's parent node nodeName and its
'value to the TreeView
Set tvwElement = tvwPeople.Nodes.Add(1, tvwChild)
tvwElement.Text = objNameNode.parentNode.nodeName & ": " _
& objNameNode.nodeTypedValue
'Add the ID of the node to the TreeView node’s tag property
'the element node is holding the id attribute that we want
'to store in the tag, as an identity reference. We
'therefore need to get hold of this node to get its value
Set objAttributes =
objDOMNode.Attributes
'check that there are attributes.
If objAttributes.length > 0 Then
'we know that we've named our id reference as
''PERSONID', therefore tell the NameNodeListMap to get
'this node by using the getNamedItem method
Set objAttributeNode =
objAttributes.getNamedItem("PERSONID")
'store this value in the tag of the TreeView
tvwElement.Tag = objAttributeNode.nodeValue
End If