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【([\wu2E80-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