根据对象的路径创建一棵树
我有一堆对象,它们不是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我编写了一些代码来帮助您将路径转变为树结构。有了这样的结构,你就可以将它转换成你需要的一切。
免责声明:我最喜欢 C#,所以这是我使用 MSDN 帮助和 VS2008 代码智能感知编写的第一个 VB.NET 程序。我仍然希望它能满足您的需求。
你的文本在 xmlString 中
这里我设计了一个代表树结构的类
最后是从 xml 代码构建树的主程序
你可以测试你的树,将这些行放在 Console.Read() 之前
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
Here I design a class that will represent a tree structure
And finaly the main program that builds a tree from your xml code
You can test your tree, put these lines right before Console.Read()