从数据库中的 URL 生成站点地图

发布于 2024-09-28 10:01:30 字数 624 浏览 6 评论 0原文

问题陈述:

URL 存储在数据库中,例如:

home/page1
gallery/image1
info/IT/contact
home/page2
home/page3
gallery/image2
info/IT/map

等等。

我想将上面的 url 排列成树形,如下所示(每个项目都是一个 url 链接)。最终输出将是一个简单的 HTML 列表(加上任何子列表),

因此:

home         gallery           info
  page1         image1           IT
  page2         image2            contact
  page3                           map

编程语言是 C# ,平台是 asp.net

编辑 1:

在上面的示例中,我们最终得到 < strong>三个列表,因为在我们的示例中存在三个主要“组”,例如:主页、画廊、信息。

当然,这可以改变,算法需要能够以某种方式递归地构建列表。

Problem Statement:

URLs are stored in a database, example:

home/page1
gallery/image1
info/IT/contact
home/page2
home/page3
gallery/image2
info/IT/map

and so on.

I would like to arrange the above urls into a tree fashion as shown below (each item will be a url link). The final output would be a simple HTML List (plus any sub list(s))

thus:

home         gallery           info
  page1         image1           IT
  page2         image2            contact
  page3                           map

Programming Language is C# , platform is asp.net

EDIT 1:

In the above example, we end up with Three Lists because in our example there is three main 'groups' eg: home, gallery, info.

Naturally, this can change, the algorithm needs to be able to somehow build the lists recursively..

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

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

发布评论

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

评论(2

零度℉ 2024-10-05 10:01:30

嗯,对这些字符串进行排序需要很多工作,我做了一些与你的情况类似的事情。我希望与你分享策略。

首先,(如果您确实可以更改表格的设计)
创建一个如下所示的表格 URL

----------------
|   URL Table  |
----------------
|  ID          | 
|  ParentID    |
|  Page        |
|..extra info..|
----------------

这是类别和子类别在同一个表格中的实现。以类似的方式,您可以包含插入很多页面和子页面。例如,

-------------------------------------
|  ID  |   ParentID   |    Page     | ...
 ------------------------------------
|   0  |     null     |    Home     |
|   1  |     null     |   Gallery   |
|   2  |     null     |    Info     |
|   3  |       0      |    Page1    |
|   4  |       0      |    Page2    |
|   5  |       0      |    Page3    | ...
|   6  |       1      |    Image1   |
|   7  |       1      |    Image2   |
|   8  |       2      |      IT     |
|   9  |       8      |    contact  |
|   1  |       8      |     map     |
------------------------------------- ...

ParentIDnull 然后是最高级别
ParentIDID 时,则它是该 ID 上任何级别的子级别,依此类推...

从 C# 方面,您知道 ParentID 为空的首页。
您可以通过选定的首页 ID 来引入它们的子页面。这是一些 ADO.NET 工作。

希望这有帮助
迈拉

Well,sorting those strings need a lot of work,I've done something similar to your condition.I wish to share the strategy with you.

First of all,(if you can change design of your tables indeed)
Create a table URL like below

----------------
|   URL Table  |
----------------
|  ID          | 
|  ParentID    |
|  Page        |
|..extra info..|
----------------

It's an implementation of category and sub category in same table.In a similar manner,you can contain insert a lot of page and subpage.For example,

-------------------------------------
|  ID  |   ParentID   |    Page     | ...
 ------------------------------------
|   0  |     null     |    Home     |
|   1  |     null     |   Gallery   |
|   2  |     null     |    Info     |
|   3  |       0      |    Page1    |
|   4  |       0      |    Page2    |
|   5  |       0      |    Page3    | ...
|   6  |       1      |    Image1   |
|   7  |       1      |    Image2   |
|   8  |       2      |      IT     |
|   9  |       8      |    contact  |
|   1  |       8      |     map     |
------------------------------------- ...

when ParentID is null then its highest level
when ParentID is and ID then its a sublevel of whatever level is on that ID and so on...

From C# side,you know the top pages where ParentID's are null.
You can bring sub pages of them by selected ID's of top pages.It's some ADO.NET work.

Hope this helps
Myra

铁轨上的流浪者 2024-10-05 10:01:30

好的,做到了:

首先创建一个类:

 public class Node
 {
    private string _Parent = string.Empty;
    private string _Child = string.Empty;
    private bool   _IsRoot = false;

    public string Parent
    {
        set { _Parent = value; }
        get { return _Parent; }
    }

    public string Child
    {
        set { _Child = value; }
        get { return _Child; }
    }

    public Node(string PChild, string PParent)
    {
        _Parent = PParent;
        _Child = PChild;
    }

    public bool IsRoot
    {
        set { _IsRoot = value; }
        get { return _IsRoot; }
    }
 }

然后生成 SiteMap,通过直接转换 url 字符串,如下所示:

   private static string MakeTree()
    {
        List<Node> __myTree = new List<Node>();

        List<string> urlRecords = new List<string>();
        urlRecords.Add("home/image1");
        urlRecords.Add("home/image2");
        urlRecords.Add("IT/contact/map");
        urlRecords.Add("IT/contact/address");
        urlRecords.Add("IT/jobs");

        __myTree = ExtractNode(urlRecords);

        List<string> __roots = new List<string>();

        foreach(Node itm in __myTree)
        {
            if (itm.IsRoot)
            {
                __roots.Add(itm.Child.ToString());
            }
        }

        string __trees = string.Empty;

        foreach (string roots in __roots)
        {
            __trees += GetChildren(roots, __myTree) + "<hr/>";
        }


        return __trees;
    }

    private static string GetChildren(string PRoot, List<Node> PList)
    {
        string __res = string.Empty;
        int __Idx = 0;

        foreach (Node x in PList)
        {
            if (x.Parent.Equals(PRoot))
            {
                __Idx += 1;
            }
        }

        if (__Idx > 0)
        {
            string RootHeader = string.Empty;

            foreach (Node x in PList)
            {
                if (x.IsRoot & PRoot == x.Child)
                {
                    RootHeader = x.Child;
                }
            }

            __res += RootHeader+ "<ul>\n";

            foreach (Node itm in PList)
            {
                if (itm.Parent.Equals(PRoot))
                {
                    __res += string.Format("<ul><li>{0}{1}</li></ul>\n", itm.Child, GetChildren(itm.Child, PList));
                }
            }
            __res += "</ul>\n";
            return __res;
        }
        return string.Empty;
    }

    private static List<Node> ExtractNode(List<string> Urls)
    {
        List<Node> __NodeList = new List<Node>();

        foreach (string itm in Urls)
        {
            string[] __arr = itm.Split('/');
            int __idx = -1;

            foreach (string node in __arr)
            {
                __idx += 1;
                if (__idx == 0)
                {
                    Node __node = new Node(node, "");
                    if (!__NodeList.Exists(x => x.Child == __node.Child & x.Parent == __node.Parent))
                    {
                        __node.IsRoot = true;
                        __NodeList.Add(__node);    
                    }
                }
                else
                {
                    Node __node = new Node(node, __arr[__idx - 1].ToString());
                    {
                        if (!__NodeList.Exists (x => x.Child == __node.Child & x.Parent == __node.Parent))
                        {
                            __NodeList.Add(__node);
                        }
                    }
                }
            }
        }
        return __NodeList;
    }

无论如何它还没有优化,我确信我可以清理它很多..

ok, did it:

First created a class:

 public class Node
 {
    private string _Parent = string.Empty;
    private string _Child = string.Empty;
    private bool   _IsRoot = false;

    public string Parent
    {
        set { _Parent = value; }
        get { return _Parent; }
    }

    public string Child
    {
        set { _Child = value; }
        get { return _Child; }
    }

    public Node(string PChild, string PParent)
    {
        _Parent = PParent;
        _Child = PChild;
    }

    public bool IsRoot
    {
        set { _IsRoot = value; }
        get { return _IsRoot; }
    }
 }

then generated the SiteMap, by transforming the urls strings directly as follows:

   private static string MakeTree()
    {
        List<Node> __myTree = new List<Node>();

        List<string> urlRecords = new List<string>();
        urlRecords.Add("home/image1");
        urlRecords.Add("home/image2");
        urlRecords.Add("IT/contact/map");
        urlRecords.Add("IT/contact/address");
        urlRecords.Add("IT/jobs");

        __myTree = ExtractNode(urlRecords);

        List<string> __roots = new List<string>();

        foreach(Node itm in __myTree)
        {
            if (itm.IsRoot)
            {
                __roots.Add(itm.Child.ToString());
            }
        }

        string __trees = string.Empty;

        foreach (string roots in __roots)
        {
            __trees += GetChildren(roots, __myTree) + "<hr/>";
        }


        return __trees;
    }

    private static string GetChildren(string PRoot, List<Node> PList)
    {
        string __res = string.Empty;
        int __Idx = 0;

        foreach (Node x in PList)
        {
            if (x.Parent.Equals(PRoot))
            {
                __Idx += 1;
            }
        }

        if (__Idx > 0)
        {
            string RootHeader = string.Empty;

            foreach (Node x in PList)
            {
                if (x.IsRoot & PRoot == x.Child)
                {
                    RootHeader = x.Child;
                }
            }

            __res += RootHeader+ "<ul>\n";

            foreach (Node itm in PList)
            {
                if (itm.Parent.Equals(PRoot))
                {
                    __res += string.Format("<ul><li>{0}{1}</li></ul>\n", itm.Child, GetChildren(itm.Child, PList));
                }
            }
            __res += "</ul>\n";
            return __res;
        }
        return string.Empty;
    }

    private static List<Node> ExtractNode(List<string> Urls)
    {
        List<Node> __NodeList = new List<Node>();

        foreach (string itm in Urls)
        {
            string[] __arr = itm.Split('/');
            int __idx = -1;

            foreach (string node in __arr)
            {
                __idx += 1;
                if (__idx == 0)
                {
                    Node __node = new Node(node, "");
                    if (!__NodeList.Exists(x => x.Child == __node.Child & x.Parent == __node.Parent))
                    {
                        __node.IsRoot = true;
                        __NodeList.Add(__node);    
                    }
                }
                else
                {
                    Node __node = new Node(node, __arr[__idx - 1].ToString());
                    {
                        if (!__NodeList.Exists (x => x.Child == __node.Child & x.Parent == __node.Parent))
                        {
                            __NodeList.Add(__node);
                        }
                    }
                }
            }
        }
        return __NodeList;
    }

anyway it's not optimised, I'm sure I can clean it up a lot..

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