Detailed description of htmlparser's visitor access method

Source: Internet
Author: User

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 ){

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.