使用Jsoup.jar寫的工具類(java提取Html中的文本)

來源:互聯網
上載者:User

標籤:

package cn.anzhuoyue.jfinalBlog.util; import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element;import org.jsoup.safety.Whitelist;import com.jfinal.kit.StringKit; public class HtmlUtil {         // 只有純文字可以通過    public static String getText(String html) {        if (html == null)            return null;        return Jsoup.clean(html, Whitelist.none());    }         // 以下標籤可以通過    // b, em, i, strong, u. 純文字    public static String getSimpleHtml(String html) {        if (html == null)            return null;        return Jsoup.clean(html, Whitelist.simpleText());    }         // 以下標籤可以通過    //a, b, blockquote, br, cite, code, dd, dl, dt, em, i, li, ol, p, pre, q, small, strike, strong, sub, sup, u, ul    public static String getBasicHtml(String html) {        if (html == null)            return null;        return Jsoup.clean(html, Whitelist.basic());    }         //在basic基礎上  增加圖片通過    public static String getBasicHtmlandimage(String html) {        if (html == null)            return null;        return Jsoup.clean(html, Whitelist.basicWithImages());    }    // 以下標籤可以通過    //a, b, blockquote, br, caption, cite, code, col, colgroup, dd, dl, dt, em, h1, h2, h3, h4, h5, h6, i, img, li, ol, p, pre, q, small, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, u, ul    public static String getFullHtml(String html) {        if (html == null)            return null;        return Jsoup.clean(html, Whitelist.relaxed());    }         //只允許指定的html標籤    public static String clearTags(String html, String ...tags) {        Whitelist wl = new Whitelist();        return Jsoup.clean(html, wl.addTags(tags));    }         // 對關鍵字加上顏色    public static String markKeywods (String keywords, String target) {        if (StringKit.notBlank(keywords)) {            String[] arr = keywords.split(" ");            for (String s : arr) {                if (StringKit.notBlank(s)) {                    String temp = "<span class=\"highlight\">" + s + "</span>";                    if(temp!=null)                        target = target.replaceAll(s, temp);                }            }        }        return target;    }     // 擷取文章中的img url    public static String getImgSrc(String html) {        if (html == null)            return null;        Document doc = Jsoup.parseBodyFragment(html);        Element image = doc.select("img").first();        return image == null ? null : image.attr("src");    }}

  

使用Jsoup.jar寫的工具類(java提取Html中的文本)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.