Lowden

now

有趣的HTMLPARSER NodeIterator测试

/*htmlparsertest.html内容:<html><head><title>This is a htmlparser test!</title></head><body><hr><br>This content that compose the body!<br></body></html><chtml><chead><ctitle>This is a htmlparser test!</ctitle></chead><cbody><hr><br>This content that compose the body!</br></cbody></chtml>
*/
测试原代码如下:

import org.htmlparser.util.NodeIterator;
import org.htmlparser.Parser;
import org.htmlparser.Node;
import org.htmlparser.util.NodeList;

public class TestNodeIterator {

    public TestNodeIterator() {
    }
    public static void main(String arsg[])throws Exception
    { 
     String path = "file:///c:/htmlparsertest.html";
     Parser parser = new Parser(path);
     
     NodeIterator ni = parser.elements();
     System.out.println("ni.hasMoreNoedes:"+ni.hasMoreNodes());       
     Node nd;
     while(ni.hasMoreNodes())
     {   nd = ni.nextNode(); 
          System.out.println("nd.getText:"+nd.getText());      
         System.out.println("ni.hasMoreNoedes:"+ni.hasMoreNodes()); 
     }       
     
    }   
   
}

测试结果如下:

ni.hasMoreNoedes:true
nd.getText:html
ni.hasMoreNoedes:true
nd.getText:chtml
ni.hasMoreNoedes:true
nd.getText:chead
ni.hasMoreNoedes:true
nd.getText:ctitle
ni.hasMoreNoedes:true
nd.getText:This is a htmlparser test!
ni.hasMoreNoedes:true
nd.getText:/ctitle
ni.hasMoreNoedes:true
nd.getText:/chead
ni.hasMoreNoedes:true
nd.getText:cbody
ni.hasMoreNoedes:true
nd.getText:hr
ni.hasMoreNoedes:true
nd.getText:br
ni.hasMoreNoedes:true
nd.getText:This content that compose the body!
ni.hasMoreNoedes:true
nd.getText:/br
ni.hasMoreNoedes:true
nd.getText:/cbody
ni.hasMoreNoedes:true
nd.getText:/chtml
ni.hasMoreNoedes:false

Process completed.

posted on 2009-06-06 09:49 Lowden 阅读(158) 评论(0)  编辑  收藏 所属分类: Java - HtmlParser

My Links

Blog Stats

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

好友链接

搜索

最新评论

阅读排行榜

评论排行榜