开发者

highlight specific word in the HTML page using JSOUP

开发者 https://www.devze.com 2023-03-16 11:59 出处:网络
Can you please tell me how to highlight specific word in the 开发者_C百科HTML page using JSOUP? Please I want it to be in JSOUP because I try to use JQuery and it causes me a lot of problem

Can you please tell me how to highlight specific word in the 开发者_C百科HTML page using JSOUP? Please I want it to be in JSOUP because I try to use JQuery and it causes me a lot of problem This is my first attempt

Elements elements=doc.getAllElements();
for (Element el:elements)
       {
    if (el.hasText())
                       {
        int pos = el.text().toUpperCase().indexOf("Hello".toUpperCase()); 
        if (pos >= 0)
                       {
            out.println(pos); // it was disappointed it gives me incorrect position 
            Element elspc = el.select("*:contains(Hello)").first();
            out.println(elspc);
            Element span = doc.createElement("span");
            span.attr("class", "highlight");
            String[] str = el.text().split("\\s");


Here is a class that I built which matches a regex and highlights it using JSOUP as parser. It is probably not the most optimal way of doing it. But it works.

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Highlighter {

    private String regex;
    private String htmlContent;
    Pattern pat;
    Matcher mat;


    public Highlighter(String searchString, String htmlString) {
        regex = buildRegexFromQuery(searchString);
        htmlContent = htmlString;
        pat = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    }

    public String getHighlightedHtml() {

        Document doc = Jsoup.parse(htmlContent);

        final List<TextNode> nodesToChange = new ArrayList<TextNode>();

        NodeTraversor nd  = new NodeTraversor(new NodeVisitor() {

            @Override
            public void tail(Node node, int depth) {
                if (node instanceof TextNode) {
                    TextNode textNode = (TextNode) node;
                    String text = textNode.getWholeText();

                    mat = pat.matcher(text);

                    if(mat.find()) {
                        nodesToChange.add(textNode);
                    }
                }
            }

            @Override
            public void head(Node node, int depth) {        
            }
        });

        nd.traverse(doc.body());

        for (TextNode textNode : nodesToChange) {
            Node newNode = buildElementForText(textNode);
            textNode.replaceWith(newNode);
        }
        return doc.toString();
    }

    private static String buildRegexFromQuery(String queryString) {
        String regex = "";
        String queryToConvert = queryString;

        /* Clean up query */

        queryToConvert = queryToConvert.replaceAll("[\\p{Punct}]*", " ");
        queryToConvert = queryToConvert.replaceAll("[\\s]*", " ");

        String[] regexArray = queryString.split(" ");

        regex = "(";
        for(int i = 0; i < regexArray.length - 1; i++) {
            String item = regexArray[i];
            regex += "(\\b)" + item + "(\\b)|";
        }

        regex += "(\\b)" + regexArray[regexArray.length - 1] + "[a-zA-Z0-9]*?(\\b))";
        return regex;
    }

    private Node buildElementForText(TextNode textNode) {
        String text = textNode.getWholeText().trim();

        ArrayList<MatchedWord> matchedWordSet = new ArrayList<MatchedWord>();

        mat = pat.matcher(text);

        while(mat.find()) {
            matchedWordSet.add(new MatchedWord(mat.start(), mat.end()));
        }

        StringBuffer newText = new StringBuffer(text);

        for(int i = matchedWordSet.size() - 1; i >= 0; i-- ) {
            String wordToReplace = newText.substring(matchedWordSet.get(i).start, matchedWordSet.get(i).end);
            wordToReplace = "<b>" + wordToReplace+ "</b>";
            newText = newText.replace(matchedWordSet.get(i).start, matchedWordSet.get(i).end, wordToReplace);       
        }
        return new DataNode(newText.toString(), textNode.baseUri());
    }

    class MatchedWord {
        public int start;
        public int end;

        public MatchedWord(int start, int end) {
            this.start = start;
            this.end = end;
        }
    }
}

Just add this in your project. And call

String htmlString = <your html content in string> 
Highlighter hl = new Highlighter("hello", htmlString);
String newhtmlString = hl.getHighlightedHtml();
0

精彩评论

暂无评论...
验证码 换一张
取 消