ITeye博客

java获取网页主信息之一:html树操作

sysu_zeh · 2007-09-01 22:17 · java学习 · HTML · 浏览器 · Java · 数据结构 · Markdown

1.节点操作

  1. package Source;
  2. //html树节点类
  3. public class Node
  4. {
  5. //构造方法
  6. public Node()
  7. {
  8. content = "";
  9. hasContent = false;
  10. parent = null;
  11. isLeaf = true;
  12. }
  13. //构造方法,初始化此节点的内容,标签,与其父辈节点
  14. public Node(String content, String tag, Node parent)
  15. {
  16. this.content = content;
  17. this.tag = tag;
  18. if(content.equalsIgnoreCase(""))
  19. hasContent = false;
  20. else
  21. hasContent = true;
  22. this.parent = parent;
  23. isLeaf = true;
  24. }
  25. //添加节点内容
  26. public void addContent(String str)
  27. {
  28. content=content+str;
  29. if(content.equalsIgnoreCase("")) hasContent = false;
  30. else hasContent = true;
  31. return;
  32. }
  33. //设置为叶子
  34. public void setLeaf(boolean is)
  35. {
  36. isLeaf = is;
  37. }
  38. //设置为块
  39. public void setBlock(boolean is)
  40. {
  41. isBlock = is;
  42. }
  43. public String toString()
  44. {
  45. return content;
  46. }
  47. String content;
  48. String tag;
  49. boolean hasContent;
  50. boolean isLeaf;
  51. boolean isBlock;
  52. Node parent;
  53. }

2.树操作

  1. package Source;
  2. import java.util.LinkedList;
  3. public class HTree
  4. {
  5. //构造方法,初始化
  6. public HTree()
  7. {
  8. list = new LinkedList();
  9. }
  10. //插入节点
  11. public void insert(Node node)
  12. {
  13. list.add(node);
  14. }
  15. //打印整棵树的节点的信息
  16. public void print()
  17. {
  18. int len = list.size();
  19. for(int i = len - 1; i >= 0; i--)
  20. {
  21. Node node = (Node)list.get(i);
  22. String str = node.content.trim();
  23. if(!str.equals("")) System.out.println(str);
  24. }
  25. }
  26. //打印块的信息
  27. public void print2()
  28. {
  29. int len = list.size();
  30. for(int i = len - 1; i >= 0; i--)
  31. {
  32. Node node = (Node)list.get(i);
  33. if(node.isBlock) System.out.println(node.content);
  34. }
  35. }
  36. //合并节点,将叶节点合并至其双亲
  37. public void merge()
  38. {
  39. int len = list.size();
  40. for(int i = len - 1; i >= 0; i--)
  41. {
  42. Node node = (Node)list.get(i);
  43. if(node.isLeaf)
  44. {
  45. Node curr = node;
  46. String str = curr.content;
  47. while(curr != null)
  48. {
  49. Node next = curr.parent;
  50. if(next != null)
  51. {
  52. if(next.hasContent)
  53. {
  54. next.addContent(str);
  55. next.setLeaf(true);
  56. node.setBlock(false);
  57. break;
  58. }
  59. next.setLeaf(false);
  60. curr = next;
  61. }
  62. else curr = null;
  63. }
  64. if(curr == null) node.setBlock(true);
  65. }
  66. }
  67. }
  68. //获取块信息
  69. public String[] getBlock()
  70. {
  71. int len = list.size();
  72. int num = 0;
  73. //获取非空节点的个数
  74. for(int i = len - 1; i >= 0; i--)
  75. {
  76. Node node = (Node)list.get(i);
  77. String str = node.content.trim();
  78. if(!str.equals("")) num++;
  79. }
  80. String contBlock[] = new String[num];
  81. num = 0;
  82. //返回信息
  83. for(int i = len - 1; i >= 0; i--)
  84. {
  85. Node node = (Node)list.get(i);
  86. String str = node.content.trim();
  87. if(!str.equals("")) contBlock[num++] = str;
  88. }
  89. return contBlock;
  90. }
  91. private LinkedList list;
  92. }

评论(2)

  1. #2 楼 · lahvey · 2009-12-12 10:57

    可以写一个浏览器了,利害

  2. #1 楼 · sku · 2008-05-25 11:38

    -zeh,你的数据结构学的太好了吧,厉害,赞一个。

评论功能维护中,当前仅展示历史评论。