【问题标题】:how to edit all text values in html tags using jsoup如何使用jsoup编辑html标签中的所有文本值
【发布时间】:2016-03-04 09:02:11
【问题描述】:

我想要什么:我是 Jsoup 的新手。我想解析我的 html 字符串并搜索出现在标签(任何标签)内的每个文本值。然后将该文本值更改为其他值。

我做了什么:我能够更改单个标签的文本值。下面是代码:

public static void main(String[] args) {
        String html = "<div><p>Test Data</p> <p>HELLO World</p></div>";
        Document doc1=Jsoup.parse(html);
        Elements ps = doc1.getElementsByTag("p");
        for (Element p : ps) {
          String pText = p.text();
          p.text(base64_Dummy(pText));
        }
        System.out.println("======================");
        String changedHTML=doc1.html();
        System.out.println(changedHTML);
    }

    public static String base64_Dummy(String abc){
        return "This is changed text";
    }

输出:

======================
<html>
 <head></head>
 <body>
  <div>
   <p>This is changed text</p> 
   <p>This is changed text</p>
  </div>
 </body>
</html>

上面的代码可以改变p标签的值。但是,在我的情况下,html 字符串可以包含任何标签;我要搜索和更改其值。 如何在 html 字符串中搜索所有标签并一一更改其文本值。

【问题讨论】:

    标签: java html jsoup


    【解决方案1】:

    您可以尝试使用类似于此代码的内容:

    String html = "<html><body><div><p>Test Data</p> <div> <p>HELLO World</p></div></div> other text</body></html>";
    
    Document doc = Jsoup.parse(html);
    List<Node> children = doc.childNodes();
    
    // We will search nodes in a breadth-first way
    Queue<Node> nodes = new ArrayDeque<>();
    
    nodes.addAll(doc.childNodes());
    
    while (!nodes.isEmpty()) {
        Node n = nodes.remove();
    
        if (n instanceof TextNode && ((TextNode) n).text().trim().length() > 0) {
            // Do whatever you want with n.
            // Here we just print its text...
            System.out.println(n.parent().nodeName()+" contains text: "+((TextNode) n).text().trim());
        } else {
            nodes.addAll(n.childNodes());
        }
    }
    

    你会得到以下输出:

    body contains text: other text
    p contains text: Test Data
    p contains text: HELLO World
    

    【讨论】:

    • 当然,你也可以使用LinkedList
    【解决方案2】:

    您想使用 CSS 选择器 * 和方法 textNodes 来获取给定标签的文本(Jsoup 世界中的 Element)。

    下面这一行

    Elements ps = doc1.getElementsByTag("p");
    

    变成

    Elements ps = doc1.select("*");
    

    现在,使用这个新的选择器,您将能够选择 HTML 代码中的任何元素(标签)。

    完整代码示例

    public static void main(String[] args) {
        System.out.println("Setup proxy...");
        JSoup.setupProxy();
    
        String html = "<html><body><div><p>Test Data</p> <div> <p>HELLO World</p></div></div> other text</body></html>";
        Document doc1 = Jsoup.parse(html);
        Elements tags = doc1.select("*");
        for (Element tag : tags) {
            for (TextNode tn : tag.textNodes()) {
                String tagText = tn.text().trim();
    
                if (tagText.length() > 0) {
                    tn.text(base64_Dummy(tagText));
                }
            }
        }
        System.out.println("======================");
        String changedHTML = doc1.html();
        System.out.println(changedHTML);
    }
    
    public static String base64_Dummy(String abc) {
        return "This is changed text";
    }
    

    输出

    ======================
    <html>
     <head></head>
     <body>
      <div>
       <p>This is changed text</p> 
       <div> 
        <p>This is changed text</p>
       </div>
      </div>This is changed text
     </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2016-04-06
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多