/*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.