Htmlparser label extraction is incomplete (htmlparser extension)

Source: Internet
Author: User
Tags lexer

Htmlparser can be used to parse HTML, but it does not recognize all tags, such as font, strong, and custom tags... the extracted content is only the starting tag of the tag.

For example, if the HTML segment is <strong> bold font </strong>, if nodelist = parser is used. parser (New tagnamefilter ("strong"); the extracted result is <strong>. if you want the extracted result to be a complete <strong> bold font </strong>, You need to extend htmlparser to let it know this label by customizing a class that inherits from htmlparser, then, use prototypicalnodefactory to register a new tag in the static code block. of course, the new label class is written in advance and the code is as follows:

/** <Br/> * @ (#): myparser. java <br/> * @ Description: <br/> * @ Author: ZX 2010-12-9 <br/> */<br/> package COM. ZX. parser; <br/> Import org.html parser. parser; <br/> Import org.html parser. prototypicalnodefactory; <br/> Import org.html parser. lexer. lexer; <br/> Import org.html parser. util. parserfeedback; <br/>/** <br/> * @ (#): myparser. java <br/> * @ Description: Class inherited from parser, extends parser <br/> * @ Author: ZX 2010-12-9 <br/> */<br/> public class myparser extends parser {<br/> Private Static prototypicalnodefactory factory = NULL; <br/> // register the custom tag <br/> static {<br/> factory = new prototypicalnodefactory (); <br/> factory. registertag (New fonttag (); <br/> factory. registertag (New strongtag (); <br/>}< br/> Public myparser () {<br/> super (); <br/> setnodefactory (factory ); <br/>}</P> <p> Public myparser (lexer, parserfeedback FB) {<br/> super (lexer, FB ); <br/> setnodefactory (factory); <br/>}</P> <p >}< br/>

 

The new extension label class is as follows:

 

/** <Br/> * @ (#): strongtag. java <br/> * @ Description: <br/> * @ Author: ZX 2010-12-9 <br/> */<br/> package COM. ZX. parser; <br/> Import org.html parser. tags. compositetag; <br/>/** <br/> * @ (#): strongtag. java <br/> * @ Description: extended strong tag <br/> * @ Author: ZX 2010-12-9 <br/> */<br/> public class strongtag extends compositetag {</P> <p> Private Static final string mids [] ={ <br/> "strong "<br/> }; <br/> Private Static final string mendtagenders [] ={< br/> "strong" <br/>}; </P> <p> Public strongtag () <br/>{< br/>}</P> <p> Public String [] getids () <br/>{< br/> return mids; <br/>}< br/> Public String [] getendtagenders () <br/>{< br/> return mendtagenders; <br/>}< br/>

 

To create an htmlparser object, you only need to create a myparser object.

Appendix

Htmlparser. jar: http://download.csdn.net/source/3099671

Htmllexer. jar: http://download.csdn.net/source/3099676

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.