HTML extract text information version-java (for Lucene indexing)

Source: Internet
Author: User
Tags gettext

Import Org.htmlparser.nodefilter;import Org.htmlparser.parser;import Org.htmlparser.beans.stringbean;import Org.htmlparser.filters.cssselectornodefilter;import Org.htmlparser.util.nodelist;public class HtmlUtil {public static string GetText (string html, string id) {try {Parser Parser = new Parser (HTML); Nodefilter filter = new Cssselectornodefilter ("#" + ID); NodeList nList = Parser.extractallnodesthatmatch (filter); return nList = = NULL | | Nlist.size () = = 0? Null:nList.elementAt (0). toplaintextstring ();} catch (Exception e) {e.printstacktrace (); return null;}} public static string Gettextbyclass (string html, string css_class) {try {Parser Parser = new Parser (HTML); Nodefilter filter = new Cssselectornodefilter ("." + Css_class); NodeList nList = Parser.extractallnodesthatmatch (filter); return nList = = NULL | | Nlist.size () = = 0? Null:nList.elementAt (0). toplaintextstring ();} catch (Exception e) {e.printstacktrace (); return null;}} public static string FilterText (string text) {if (text = = null)return null;text = Text.replace (">", ">"), Text = Text.replace ("<", "<"), Text = Text.replace ("" "," \ ""); text = Text.replace (" ", ""); text = Text.replace ("&", "&"); text = Text.replace ("&copy;", "©"); text = Text.rep Lace ("", ""); return text;} /** * Get plain text information in Web pages * * @param HTML * @param ID * @return * @throws Exception * @throws Exception */public static String get Text (String html) throws Exception {Stringbean bean = new Stringbean (); Bean.setlinks (false); Bean.setreplacenonbreakingspaces (True); Bean.setcollapse (true);//Returns the parsed page plain text information parser parser = Parser.createparser ( HTML, "Utf-8");p Arser.visitallnodeswith (Bean);p arser.reset (); return bean.getstrings ();}}

Need to use the Htmlparse.jar library, called the following way:

Htmlutil.gettext (HTMLSTR);

HTML extract text information version-java (for Lucene indexing)

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.