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

关键字: html
1.节点操作

[code]
package Source;


//html树节点类
public class Node
{
//构造方法
public Node()
{
content = "";
hasContent = false;
parent = null;
isLeaf = true;
}

//构造方法,初始化此节点的内容,标签,与其父辈节点
public Node(String content, String tag, Node parent)
{
this.content = content;
this.tag = tag;
if(content.equalsIgnoreCase(""))
hasContent = false;
else
hasContent = true;
this.parent = parent;
isLeaf = true;
}

//添加节点内容
public void addContent(String str)
{
content=content+str;
if(content.equalsIgnoreCase("")) hasContent = false;
else hasContent = true;
return;
}

//设置为叶子
public void setLeaf(boolean is)
{
isLeaf = is;
}

//设置为块
public void setBlock(boolean is)
{
isBlock = is;
}

public String toString()
{
return content;
}

String content;
String tag;
boolean hasContent;
boolean isLeaf;
boolean isBlock;
Node parent;
}
[/code]

2.树操作

[code]
package Source;

import java.util.LinkedList;


public class HTree
{
//构造方法,初始化
public HTree()
{
list = new LinkedList();
}

//插入节点
public void insert(Node node)
{
list.add(node);
}


//打印整棵树的节点的信息
public void print()
{
int len = list.size();
for(int i = len - 1; i >= 0; i--)
{
Node node = (Node)list.get(i);
String str = node.content.trim();
if(!str.equals("")) System.out.println(str);
}

}

//打印块的信息
public void print2()
{
int len = list.size();
for(int i = len - 1; i >= 0; i--)
{
Node node = (Node)list.get(i);
if(node.isBlock) System.out.println(node.content);
}

}

//合并节点,将叶节点合并至其双亲
public void merge()
{
int len = list.size();
for(int i = len - 1; i >= 0; i--)
{
Node node = (Node)list.get(i);
if(node.isLeaf)
{
Node curr = node;
String str = curr.content;
while(curr != null)
{
Node next = curr.parent;
if(next != null)
{
if(next.hasContent)
{
next.addContent(str);
next.setLeaf(true);
node.setBlock(false);
break;
}
next.setLeaf(false);
curr = next;
}
else curr = null;
}
if(curr == null) node.setBlock(true);
}
}

}

//获取块信息
public String[] getBlock()
{
int len = list.size();
int num = 0;
//获取非空节点的个数
for(int i = len - 1; i >= 0; i--)
{
Node node = (Node)list.get(i);
String str = node.content.trim();
if(!str.equals("")) num++;
}

String contBlock[] = new String[num];
num = 0;

//返回信息
for(int i = len - 1; i >= 0; i--)
{
Node node = (Node)list.get(i);
String str = node.content.trim();
if(!str.equals("")) contBlock[num++] = str;
}
return contBlock;
}

private LinkedList list;
}
[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值