Solved

Tree view from XML in Visual Basic

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

757 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

19 Experts available now in Live!

Get 1:1 Help Now