Javascript 带有JSOUP的HTML注释

Javascript 带有JSOUP的HTML注释,javascript,html,css,annotations,jsoup,Javascript,Html,Css,Annotations,Jsoup,我需要根据网页的文本内容自动为网页添加注释。例如,我想在有城市的地方注释html内容,并添加一个跨度,如: 巴塞罗那-->巴塞罗那 问题是我知道城市的位置是基于文本内容,而不是基于html。以前我曾与JSoup合作过,但我找不到如何根据文本位置找到插入标记的位置。以下是实现目标的方法: public static void main(String[] args) { Document doc = Jsoup.parse("<p>Barcelona is a nice plac

我需要根据网页的文本内容自动为网页添加注释。例如,我想在有城市的地方注释html内容,并添加一个跨度,如:

巴塞罗那-->巴塞罗那


问题是我知道城市的位置是基于文本内容,而不是基于html。以前我曾与JSoup合作过,但我找不到如何根据文本位置找到插入标记的位置。

以下是实现目标的方法:

public static void main(String[] args) {
    Document doc = Jsoup.parse("<p>Barcelona is a nice place to live !<br/>Other cities <b>too</b>!</p>");
    dumpDocument("** BEFORE **", doc);

    Matcher replacer = Pattern.compile("(?i)(barcelona)").matcher("");
    for (Element elt : doc.body().select("*")) {
        for (TextNode textNode : elt.textNodes()) {
            String originalText = textNode.text();

            if (replacer.reset(originalText).find()) {
                String annotatedHtml = replacer.replaceAll("<span class=\"city\">$1</span>");
                textNode.before(annotatedHtml);
                textNode.remove();
            }
        }
    }
    dumpDocument("** AFTER **", doc);
}

private static void dumpDocument(String title, Document doc) {
    System.out.println(title);
    System.out.println(doc.html());
    System.out.println();
}
publicstaticvoidmain(字符串[]args){
Document doc=Jsoup.parse(“巴塞罗那是一个居住的好地方!
其他城市也一样!

”; 转储文件(“**前**”,doc); Matcher replacer=Pattern.compile(“(?i)(巴塞罗那)”).Matcher(“”); 对于(元素elt:doc.body()。选择(“*”){ 对于(TextNode TextNode:elt.textNodes()){ String originalText=textNode.text(); if(replacer.reset(originalText.find()){ 字符串注释DHTML=replacer.replaceAll($1); textNode.before(dhtml); textNode.remove(); } } } 文件(在**之后为“**号”,文件); } 私有静态无效转储文档(字符串标题、文档文档){ 系统输出打印项次(标题); System.out.println(doc.html()); System.out.println(); }
输出

**之前**
巴塞罗那是一个居住的好地方<其他城市也一样

**之后** 巴塞罗那是一个居住的好地方<其他城市也一样


以下是实现目标的方法:

public static void main(String[] args) {
    Document doc = Jsoup.parse("<p>Barcelona is a nice place to live !<br/>Other cities <b>too</b>!</p>");
    dumpDocument("** BEFORE **", doc);

    Matcher replacer = Pattern.compile("(?i)(barcelona)").matcher("");
    for (Element elt : doc.body().select("*")) {
        for (TextNode textNode : elt.textNodes()) {
            String originalText = textNode.text();

            if (replacer.reset(originalText).find()) {
                String annotatedHtml = replacer.replaceAll("<span class=\"city\">$1</span>");
                textNode.before(annotatedHtml);
                textNode.remove();
            }
        }
    }
    dumpDocument("** AFTER **", doc);
}

private static void dumpDocument(String title, Document doc) {
    System.out.println(title);
    System.out.println(doc.html());
    System.out.println();
}
publicstaticvoidmain(字符串[]args){
Document doc=Jsoup.parse(“巴塞罗那是一个居住的好地方!
其他城市也一样!

”; 转储文件(“**前**”,doc); Matcher replacer=Pattern.compile(“(?i)(巴塞罗那)”).Matcher(“”); 对于(元素elt:doc.body()。选择(“*”){ 对于(TextNode TextNode:elt.textNodes()){ String originalText=textNode.text(); if(replacer.reset(originalText.find()){ 字符串注释DHTML=replacer.replaceAll($1); textNode.before(dhtml); textNode.remove(); } } } 文件(在**之后为“**号”,文件); } 私有静态无效转储文档(字符串标题、文档文档){ 系统输出打印项次(标题); System.out.println(doc.html()); System.out.println(); }
输出

**之前**
巴塞罗那是一个居住的好地方<其他城市也一样

**之后** 巴塞罗那是一个居住的好地方<其他城市也一样


Hi,如果您还可以使用预期输出添加整个HTML内容,那就太好了。Hi,如果您还可以使用预期输出添加整个HTML内容,那就太好了。