/**
* @(#)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();
}
}
}