Solved

Tree view from XML in Visual Basic

Posted on 2004-08-12
7
215 Views
Last Modified: 2013-11-25
<Subject>
 <Level1 NAME="Accounting" ID="1680" Level="1" Status="Active" HasChild="True">
        <Level2 NAME="Accounting1" ID="1681" Level="2" Status="Active" ParentID="1680" HasChild="False" />
          <Level2 NAME="Accounting2" ID="1682" Level="2" Status="Active" ParentID="1680" HasChild="False" />
        <Level2 NAME="Additional3" ID="1683" Level="2" Status="Active" ParentID="1680" HasChild="False" />
 </Level1>

 <Level1 NAME="Auditing" ID="1775" Level="1" Status="Active" HasChild="True">
      <Level2 NAME="Analytical" ID="1776" Level="2" Status="Active" ParentID="1775" HasChild="False" />
        <Level2 NAME="Appointment" ID="1777" Level="2" Status="Active" ParentID="1775"       HasChild="False" />
        <Level2 NAME="Attests" ID="1778" Level="2" Status="Active" PID="1775" HasChild="False" />
 </Level1>

 <Level1 NAME="Regulatory" ID="1810" Level="1" Status="Active" HasChild="True">
        <Level2 NAME="Accounting" ID="1682" Level="2" Status="Active" PID="1810" HasChild="False" />
        <Level2 NAME="Audit" ID="1779" Level="2" Status="Active" PID="1810" HasChild="False" />
        <Level2 NAME="Business" ID="1693" Level="2" Status="Active" PID="1810" HasChild="False" />
        <Level2 NAME="Certification" ID="1809" Level="2" Status="Active" PID="1810" HasChild="False" />
  </Level1>
</subject>


I have an XML File like above.It is an heirarchial data.I need to create a tree view in visual Basic 6.0 by reading XML file.
The tree should look like,


Subject
|
 _ Accounting      
      |_Accounting1
      |_Accounting2
      |-Additional3

|_Auditing
      |_ Analytical
      |_ Appointment
      |_ Attests

|_ Regulatory
      |_Accounting
      |_Audit
      |_Business


Suggestion anyone! Thnaks in advance.
0
Comment
Question by:sormv
  • 2
  • 2
7 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 63 total points
ID: 11789109
What you need is a bit of good old recurison :o)

Try this as a sample:

    Private Sub Form_Load()

        Dim oDom As New DOMDocument40
        oDom.Load "C:\Temp.xml"

        Call RecurseNodes(oDom.documentElement, 1)

    End Sub

    Private Sub RecurseNodes(ByVal pNode As IXMLDOMNode, ByVal pLevel As Integer, Optional ByVal pIndex As Integer)

       Dim oTNode As MSComctlLib.Node
       Dim oNode As IXMLDOMNode
   
        If pLevel = 1 Then
            Set oTNode = tvwMain.Nodes.Add(, , , pNode.nodeName)
        Else
            Set oTNode = tvwMain.Nodes.Add(tvwMain.Nodes(pIndex), tvwChild, , pNode.selectSingleNode("@NAME").Text)
        End If

        For Each oNode In pNode.childNodes
            Call RecurseNodes(oNode, pLevel + 1, oTNode.Index)
        Next oNode

    End Sub

You will need to add a reference to "Microsoft XML 4.0", and it also assumes a treeview named tvwMain.

If theres anything you need explaining further, just ask.

Hope this helps.
0
 
LVL 22

Assisted Solution

by:DarkoLord
DarkoLord earned 62 total points
ID: 11789123
Well this is far simpler and you don't need any reference:

Private Sub Command1_Click()
Dim strTmp As String, l1() As String, l2() As String, i As Long, j As Long, tmpParent As String, tmpChild As String
Dim FF As Integer
 FF = FreeFile
 Open "c:\data.xml" For Input As #FF
  strTmp = Input(LOF(FF), FF)
 Close #FF

l1 = Split(strTmp, "<Level1 ")
For i = 1 To UBound(l1)
    tmpParent = Mid$(l1(i), 7, InStr(7, l1(i), """") - 7)
    TreeView1.Nodes.Add , , tmpParent, tmpParent
    l2 = Split(l1(i), "<Level2 ")
    For j = 1 To UBound(l2)
        tmpChild = Mid$(l2(j), 7, InStr(7, l2(j), """") - 7)
        TreeView1.Nodes.Add tmpParent, tvwChild, tmpParent & "-" & tmpChild, tmpChild
    Next j
Next i

End Sub

Darko
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 11790762
It may be simpler, but it doesn't currently cater for the top level node and its also highly inflexible. When dealing with XML its better to use the DOM, thats what its there for.
0
 
LVL 22

Expert Comment

by:DarkoLord
ID: 11791628
True, but if XML files dont change dramatically this code works perfectly... I have been using this kind of parsing for some time now and there was not a single problem with it... ;)

Darko
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question