Solved

Tree view from XML in Visual Basic

Posted on 2004-08-12
7
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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 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 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…
Suggested Courses
Course of the Month5 days, 16 hours left to enroll

627 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