编程语言
首页 > 编程语言> > java 读取html字符串替换字符

java 读取html字符串替换字符

作者:互联网

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


/**
 * 数据替换
 * @param dbInfo
 * @param htmlStr
 * @return
 */
public String htmlReplace(BasicDBObject dbInfo, String htmlStr) {
    Document doc = Jsoup.parse(htmlStr);
    Elements es = doc.select("div");
    for (Element element : es) {
        String divStr = element.html();
        if (divStr.indexOf("@html") > 0) {
            Elements esSub = element.children();
            for (Element element2 : esSub) {
                String name = element2.html();
                String divStrSub = element2.toString();
                Pattern    pattern = Pattern.compile("\\@html【([\\w\u2E80-\uFE4F]*)】");
                Matcher    matcher = pattern.matcher(divStrSub);
                while(matcher.find()) {
                    htmlStr = htmlStr.replace(name, dbInfo.getString(matcher.group(1)));
                }
            }
        }
    }
    return htmlStr;
}

jar包:jsoup-1.12.1.jar  下载:https://jsoup.org/download

标签:java,String,import,jsoup,html,htmlStr,org,读取
来源: https://www.cnblogs.com/BobXie85/p/11542202.html