Jsoup 在没有选择器的情况下从 div 中恢复文本

Jsoup recove text from div without selector

我有这个HTML代码

<div itemprop="doseSchedule">
text1
</div>
<h3><a id="SP3">TITLE</a></h3>
<div>text2</div>
<div itemprop="warning">
text3
</div>

我尝试恢复 text2,但仍然无法恢复。我该怎么做?

此代码将执行您想要的操作:

public class JSoupTest {
   public static void main(String[] args) throws IOException {
    String html = "<div itemprop=\"doseSchedule\">\n"
            + "text1\n"
            + "</div>\n"
            + "<h3><a id=\"SP3\">TITLE</a></h3>\n"
            + "<div>text2</div>\n"
            + "<div itemprop=\"warning\">\n"
            + "text3\n"
            + "</div>";

    Document doc = Jsoup.parse(html);

    Element e = doc.select("h3").first().nextElementSibling();

    System.out.println(e.text());
    }
}