Lowden

now

关于HTMLPARSER中的Node 的测试

/**
 * @(#)NodeTest.java
 *
 *
 * @author
 * @version 1.00 2009/6/17
 */

import org.htmlparser.Parser;
import org.htmlparser.util.NodeList;
import org.htmlparser.Node;
import org.htmlparser.nodes.*;

public class NodeTest {

    public NodeTest() {
    }
 
 public static void main(String args[]){
  String path = "file:///c:/htmlparsertest.html";
  try{  
   Parser pr = new Parser(path);
   NodeList nl = pr.parse(null);//生成结点链
  // System.out.println(nl.size());
  // TextNode[] nodearray={null,null,null,null,null,null,null,null,null,null,null,null,null,null};
  // nl.copyToNodeArray(Node[] array);//copyToNodeArray失败
  // Node temp;
  String parent,sibling,child;
   for(int i=0;i<nl.size();i++){
    System.out.println(nl.size());    
    if(nl.elementAt(i) instanceof TagNode){
     System.out.print("TagNode==>"); 
    }
    else if(nl.elementAt(i) instanceof RemarkNode){
     System.out.print("RemarkNode==>");
    }
    else if(nl.elementAt(i) instanceof TextNode){
     System.out.print("TextNode==>");
    }    
     
     
    if(nl.elementAt(i).getParent()==null){
     parent="null";
    }   
    else{
     parent=nl.elementAt(i).getParent().getText();
    }
    
    
    if(nl.elementAt(i).getNextSibling()==null){
     sibling="null";     
    }   
    else{
     sibling=nl.elementAt(i).getNextSibling().getText();
    }
    
    
    if(nl.elementAt(i).getChildren()==null){
     child="null";
    }   
    else{
     child=nl.elementAt(i).getFirstChild().getText();
    }
    
    System.out.println("nl.elementAt("+i+").getText()="+nl.elementAt(i).getText()+"   Parent is :"+parent+",NextSibling is :"+sibling+",First Child is:"+child);
    
    if(nl.elementAt(i).getNextSibling()==null&&nl.elementAt(i).getChildren()!=null){
     nl=nl.elementAt(i).getChildren();
     i=0;
    }
    
   }
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }   
}

posted on 2009-06-20 17:31 Lowden 阅读(169) 评论(0)  编辑  收藏 所属分类: Java - HtmlParser

My Links

Blog Stats

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

好友链接

搜索

最新评论

阅读排行榜

评论排行榜