我如何取代“文字”在使用Jsoup的每个标签中 [英] How I can replace "text" in the each tag using Jsoup

查看:93
本文介绍了我如何取代“文字”在使用Jsoup的每个标签中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下html:

I have the following html:

<html>
<head>
</head>
<body>
    <div id="content" >
         <p>text <strong>text</strong> text <em>text</em> text </p>
    </div>
</body>    
</html>

如何在每个标签中使用 Jsoup 库。
我想看看:

How I can replace "text" to "word" in the each tag using Jsoup library. I want to see:

<html>
<head>
</head>
<body>
    <div id="content" >
         <p>word <strong>word</strong> word <em>word</em> word </p>
    </div>
</body>    
</html>

谢谢您的任何建议!

UPD:
感谢您的答案,但我找到了多种方式:

UPD: Thanks for answers, but I found the versatile way:

    Element entry = doc.select("div").first();
    Elements tags = entry.getAllElements();
    for (Element tag : tags) {
        for (Node child : tag.childNodes()) {
            if (child instanceof TextNode && !((TextNode) child).isBlank()) {
                System.out.println(child); //text
                ((TextNode) child).text("word"); //replace to word
            }
        }
    }


推荐答案

Document doc = Jsoup.connect(url).get();
String str = doc.toString();
str = str.replace("text", "word");

尝试一下..

这篇关于我如何取代“文字”在使用Jsoup的每个标签中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆