Reference document: http://allenj2ee.iteye.com/blog/222457, hope to share with you
Simply and unilaterally, filter filters are used to filter out the desired node based on certain conditions and then process it. Visitor traverses every node in the content tree and processes nodes that meet the conditions.
. The actual results are the same. Two different methods can achieve the same results:
1,
Package HTML;
Import java. Io. ioexception;
Import java.net. httpurlconnection;
Import java.net. malformedurlexception;
Import java.net. url;
Import org.html parser. parser;
Import org.html parser. remark;
Import org.html parser. Tag;
Import org.html parser. text;
Import org.html parser. util. parserexception;
Import org.html parser. Visitors. nodevisitor;
Public class testvisitor {
Public static void testvisitor (string URL ){
Try {
// =================== Connection method 1 ========================================
URL l_url = new java.net. URL (URL );
Httpurlconnection l_connection = (httpurlconnection) l_url.openconnection ();
// Create a parser object by specifying the urlconnection object
Parser = new Parser (l_connection );
// =================== Connection method 2 ==================================
// Establish a connection
// Httpurlconnection l_connection = (httpurlconnection) l_url.openconnection ();
// Create a parser object by specifying the urlconnection object
// Parser = new Parser ();
// Parser. seturl (URL );
Parser. setencoding (parser. getencoding ());
Nodevisitor visitor = new nodevisitor (true, false ){
Public void visittag (TAG tag ){
System. Out. println ("tagname =" + tag. gettagname () + "-- text =" + tag. gettext () + "--
Class = "+ tag. getpage ());
}
Public void visitstringnode (text string ){
System. Out. println ("this is text:" + String );
}
Public void visitremarknode (Remark remark ){
System. Out. println ("this is remark:" + remark. gettext ());
}
Public void beginparsing (){
System. Out. println ("beginparsing ");
}
Public void visitendtag (TAG tag ){
System. Out. println ("visitendtag:" + tag. gettext ());
}
Public void finishedparsing (){
System. Out. println ("finishedparsing ");
}
};
Parser. visitallnodeswith (visitor );
} Catch (exception e ){
E. printstacktrace ();
}
}
Public static void main (string [] ARGs ){
Testvisitor ("http://www.hao123.com /");
}
}
2. You can see that before you start traversing the nodes, beginparsing is called first, and then the intermediate node is processed. Finally, before the traversal ends, finishparsing is called. Because I
Both the configured recursechildren and recurseself are false. Therefore, the visitor does not access the child node or the root node.
3. Set recurseself to true to see what will happen.
Nodevisitor visitor = new nodevisitor (false, true ){
We can see that the first layer of the HTML page is called.
4. Let's call the following method:
Nodevisitor visitor = new nodevisitor (true, false)
We can see that all the subnodes appear, except the two top-level nodes in the preceding example: this is Tag: Head and this is Tag: HTML.
To make them all out, you only need
Nodevisitor visitor = new nodevisitor (True, true ){