выделите определенное слово на странице HTML с помощью JSOUP - PullRequest
1 голос
/ 01 июля 2011

Подскажите, пожалуйста, как выделить определенное слово на странице HTML с помощью JSOUP? Пожалуйста, я хочу, чтобы он был в JSOUP, потому что я пытаюсь использовать JQuery, и это вызывает у меня много проблем Это моя первая попытка

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");

1 Ответ

2 голосов
/ 23 декабря 2011

Вот класс, который я построил, который соответствует регулярному выражению и выделяет его, используя JSOUP в качестве парсера.Это, вероятно, не самый оптимальный способ сделать это.Но это работает.

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;
        }
    }
}

Просто добавьте это в свой проект.И позвони

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