Lowden

now

Node和Tag综合测试

/**
 * @(#)DiGuiTest.java
 *
 *
 * @author
 * @version 1.00 2009/6/17
 */
import java.lang.*;
import org.htmlparser.Parser;
import org.htmlparser.Node;
import org.htmlparser.util.NodeList;
import org.htmlparser.nodes.*;
import org.htmlparser.tags.*;
import org.htmlparser.tags.AppletTag;

 

public class DiGuiTest {
 
    public DiGuiTest() {
    }
   
    public static void main(String args[]){
  String path = "file:///c:/htmlparsertest.html";
  try{
  
   Parser pr = new Parser();
   pr.setURL(path);
   NodeList nl = pr.parse(null);
   DiGuiTest kk = new DiGuiTest();
   for(int i=0;i<nl.size();i++){
    
    if(nl.elementAt(i) instanceof TagNode){
     System.out.print("TagNode==>"); 
      if(nl.elementAt(i) instanceof LinkTag ){       
       System.out.print("LinkTag==>");
      }
      else if(nl.elementAt(i) instanceof ImageTag){
       System.out.print("ImageTag==>");
      }
      else if(nl.elementAt(i) instanceof ScriptTag){
       System.out.print("ScriptTag==>");
      }
      else{
       System.out.print("OtherTag-->");
      }
    }
    else if(nl.elementAt(i) instanceof RemarkNode){
     System.out.print("RemarkNode==>");
    }
    else if(nl.elementAt(i) instanceof TextNode){
     System.out.print("TextNode==>");
    }
    
    kk.PrintNode(nl.elementAt(i),0); 
   }
   
  }
  catch(Exception e){
   e.printStackTrace();
  }
    }
   
   
    public void PrintNode(Node temp,int cout){
     try{
      if(temp!=null){
       System.out.println("Node :"+temp.getText()+"'s deepth is "+cout);
       if(temp.getChildren()!=null){
      
        NodeList nl = temp.getChildren();
       
        for(int i= 0;i<nl.size();i++){
         if(nl.elementAt(i) instanceof TagNode){
       System.out.print("TagNode==>"); 
        
       if(nl.elementAt(i) instanceof LinkTag ){
        System.out.print("LinkTag==>");
        LinkTag lt =(LinkTag)nl.elementAt(i);
        System.out.print("  LinkTag:extractLink is:"+lt.extractLink());
        System.out.print("  getAccessKey"+lt.getAccessKey());
       }
       else if(nl.elementAt(i) instanceof ImageTag){
        ImageTag it = (ImageTag)nl.elementAt(i);
        System.out.print("ImageTag==>");
        System.out.print("  getImageURL():"+it.getImageURL());
        }
       else if(nl.elementAt(i) instanceof ScriptTag){
        System.out.print("ScriptTag==>");
       }
       else{
        System.out.print("OtherTag-->");
       }
      }
      
      else if(nl.elementAt(i) instanceof RemarkNode){
       System.out.print("RemarkNode==>");
      }
      else if(nl.elementAt(i) instanceof TextNode){
       System.out.print("TextNode==>");
      /* if((TextNode)(nl.elementAt(i)).isWhiteSpace()){
        nl.elementAt(i).setText("WhiteSpace");
       }
       */
      }
      
         if(nl.elementAt(i)!=null){       
          PrintNode(nl.elementAt(i),cout+1);
         }
        }
       }
      } 
     } 
     catch(Exception e){
      e.printStackTrace();
     } 
    }
 
   
}

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

My Links

Blog Stats

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

好友链接

搜索

最新评论

阅读排行榜

评论排行榜