Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Tree view from XML in Visual Basic

Posted on 2004-08-12
7
Medium Priority
?
223 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 252 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 248 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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 …
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

705 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