Solved

Tree view from XML in Visual Basic

Posted on 2004-08-12
7
209 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now