---
title: "java获取网页主信息之一:html树操作"
author: "sysu_zeh"
date: "2007-09-01T22:17:37Z"
tags: ["HTML", "Java", "数据结构", "浏览器"]
slug: "java-html"
draft: false
lastmod: "2007-09-01T22:17:37Z"
id: 119313
url: "https://sysu-zeh.iteye.com/blog/119313"
---

**1.节点操作**

01. package Source;
02. //html树节点类
03. public class Node
04. {
05. //构造方法
06. public Node()
07. {
08. content = "";
09. 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.树操作**

01. package Source;
02. import java.util.LinkedList;
03. public class HTree
04. {
05. //构造方法,初始化
06. public HTree()
07. {
08. list = new LinkedList();
09. }
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 &gt;= 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 &gt;= 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 &gt;= 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 &gt;= 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 &gt;= 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. }