How I can replace “text” in the each tag using Jsoup

后端 未结 3 1110
粉色の甜心
粉色の甜心 2021-01-22 09:54

I have the following html:





    

text text

相关标签:
3条回答
  • 2021-01-22 10:30

    A quick search turned up this code:

    Elements strongs = doc.select("strong");
    Element f = strongs.first();
    Element l = strongs.last();1,siblings.lastIndexOf(l));
    

    etc

    First what you want to do is understand how the library works and what features it contains, and then you figure out how to use the library to do what you need. The code above seems to allow you to select a strong element, at which point you could update it's inner text, but I'm sure there are a number of ways you could accomplish the same.

    In general, most libraries which parse xml are able to select any given element in the document object model, or any list of elements, and either manipulate the elements themselves, or their inner text, attributes and the like.

    Once you gain more experience working with different libraries, your starting point is to look for the documentation of the library to see what that library does. If you see a method that says it does something, that's what it does, and you can expect to use it to accomplish that goal. Then, instead of writing a question on Stack Overflow, you just need to parse the functionality of the library you're using, and figure out how to use it to do what you want.

    0 讨论(0)
  • 2021-01-22 10:31
        String html = "<html> ...";
        Document doc = Jsoup.parse(html);
        Elements p = doc.select("div#content > p");
        p.html(p.html().replaceAll("text", "word"));
        System.out.println(doc.toString());
    

    div#content > p means that the elements <p> in the element <div> which id is content.

    If you want to replace the text only in <strong>text</strong>:

        Elements p = doc.select("div#content > p > strong");
        p.html(p.html().replaceAll("text", "word"));
    
    0 讨论(0)
  • 2021-01-22 10:43
    Document doc = Jsoup.connect(url).get();
    String str = doc.toString();
    str = str.replace("text", "word");
    

    try it..

    0 讨论(0)
提交回复
热议问题