根据对象的路径创建树

时间:2011-09-20 04:40:40

标签: vb.net jstree web

我有一堆对象,它们不是XML,但xml文档看起来像这样:

    <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>

这棵树显然非常深。有没有人有算法将其创建为jstree,“UL”和“LI”系列。如果你在vb中有代码那将是圣诞节......但我会对逻辑感到满意。我的想法是最终将它变成一个jsonp Web服务,所以我可以使用jstree来构建一个树,但是现在我只是想了解正确解析它所需的逻辑。

谢谢!

1 个答案:

答案 0 :(得分:1)

我编写了一些代码,可以帮助您将路径转换为树结构。拥有这样的结构,你可以将它转换成你需要的一切。

免责声明:我主要喜欢C#,所以这是我用VBDN编写的第一个用MSDN帮助和VS2008代码智能感知的程序。我仍然希望它能满足您的需求。

您的文字位于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>"

这里我设计了一个代表树结构的类

   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

最终是从xml代码构建树的主程序

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

您可以测试树,将这些行放在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