开发者

Create a tree based on path of the object

开发者 https://www.devze.com 2023-04-05 20:31 出处:网络
I have a bunch of objects, they aren\'t in XML but the xml document would look like this: <object>

I have a bunch of objects, they aren't in XML but the xml document would look like this:

    <object>
       <path>root</path>
    </object>
    <object>
       <path>root/sub1</path>
    </object>
    <object>
       <path>root/sub1/item1</path>
    </object>
    <object>
       <path>root/sub1/item2</path>
    </object>
    <object>
       <path>root/sub2</path>
    </object>
    <object>
       <path>root/sub2/item1</path>
    </object>
    <object>
       <path>root/sub2/item2</path>
    </object>

The tree is obviously v开发者_C百科ery deep. Does anyone have an algorithm to create this into a jstree, series of "UL" and "LI". It would be christmas if you had the code in vb...but I will be satisfied with just the logic. My thought is to eventually make this into a jsonp web service so I can use jstree to build a tree, but for now im just trying to understand the logic necessary to parse this correctly.

Thanks!


I wrote some code that will help you turn you path into a tree structure. And having such s structure you can convert it into everything what you need.

Disclaimer: I'm mostly fond of C# so this is my first program in VB.NET written with MSDN help and VS2008 code intellisense. Still I hope it will serve your needs.

Your text is in xmlString

    Private xmlString = "<root> " + _
" <object> " + _
   "<path>root</path>" + _
" </object>" + _
" <object>" + _
   "<path>root/sub1</path>" + _
" </object>" + _
" <object>" + _
   "<path>root/sub1/item1</path>" + _
" </object>" + _
" <object>" + _
   "<path>root/sub1/item2</path>" + _
" </object>" + _
" <object>" + _
   "<path>root/sub2</path>" + _
" </object>" + _
" <object>" + _
   "<path>root/sub2/item1</path>" + _
" </object>" + _
" <object>" + _
   "<path>root/sub2/item2</path>" + _
" </object>" + _
"</root>"

Here I design a class that will represent a tree structure

   Class TreeNode
        Public NodeName As String
        Public Children As List(Of TreeNode)

        Public Sub New(ByVal ANodeName As String)
            NodeName = ANodeName
            Children = New List(Of TreeNode)

        End Sub

        Public Function AddNodeIfNotExists(ByVal ANodeName As String) As TreeNode
            For Each node As TreeNode In Children
                If node.NodeName.Equals(ANodeName) Then
                    Return node

                End If
            Next
            Dim newnode As TreeNode = New TreeNode(ANodeName)

            Children.Add(newnode)
            Return newnode

        End Function
    End Class

And finaly the main program that builds a tree from your xml code

Sub Main()
    Dim doc As XmlDocument
    doc = New XmlDocument()
    doc.LoadXml(xmlString)
    Dim nodes As XmlNodeList
    nodes = doc.DocumentElement.GetElementsByTagName("object")

    Dim Tree As TreeNode = New TreeNode("global-root")
    Dim Slider As TreeNode = Tree

    For Each node As XmlNode In nodes
        Dim s As String = node.SelectSingleNode("path").InnerText
        Dim routes = s.Split("/")
        For Each route As String In routes
            Slider = Slider.AddNodeIfNotExists(route)

        Next
        Slider = Tree

    Next



        Console.Read()

    End Sub

You can test your tree, put these lines right before Console.Read()

'Test your tree
        Console.WriteLine(Tree.Children(0).NodeName = "root") 'true
        Console.WriteLine(Tree.Children(0).Children(0).NodeName = "sub1") 'true
        Console.WriteLine(Tree.Children(0).Children(0).Children(0).NodeName = "item1") 'true
        Console.WriteLine(Tree.Children(0).Children(0).Children(1).NodeName = "item2") 'true



        Console.WriteLine(Tree.Children(0).Children(1).NodeName = "sub2") 'true
        Console.WriteLine(Tree.Children(0).Children(1).Children(0).NodeName = "item1") 'true
        Console.WriteLine(Tree.Children(0).Children(1).Children(1).NodeName = "item2") 'true
0

精彩评论

暂无评论...
验证码 换一张
取 消