代码之家  ›  专栏  ›  技术社区  ›  Darknight

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

  •  0
  • Darknight  · 技术社区  · 14 年前

    问题陈述:

    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:

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

    2 回复  |  直到 14 年前
        1
  •  0
  •   Myra    14 年前

    好吧,整理这些字符串需要很多工作,我已经做了一些类似于你的情况的工作。我想和你分享一下策略。

    首先,(如果你真的能改变你的桌子的设计)
    创建如下表URL

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

    它是同一个表中category和sub category的实现,同样可以包含insert很多page和subpage,例如:,

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

    什么时候 无效的 那么它的最高水平
    什么时候 是和 身份证件 那么它是身份证上任何级别的子级别等等。。。

    从C#端,您知道ParentID为空的顶部页面。
    你可以根据所选的首页的ID带出它们的子页面,这是ADO.NET的一部分。


    玛拉

        2
  •  0
  •   Darknight    14 年前

    好吧,是不是:

    首先创建一个类:

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

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

    不管怎么说,这是不乐观的,我相信我可以清理它很多。。

    推荐文章