根据对象的路径创建一棵树

发布于 2024-12-05 08:14:18 字数 820 浏览 0 评论 0原文

我有一堆对象,它们不是 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 来构建一棵树,但现在我只是想了解正确解析它所需的逻辑。

谢谢!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤寂小茶 2024-12-12 08:14:18

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

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

你的文本在 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文