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

1.节点操作

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

2.树操作

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值