编程语言
首页 > 编程语言> > Java jsoup-清除链接以外的所有标签

Java jsoup-清除链接以外的所有标签

作者:互联网

输入字符串:

< b>测试链接< / b> < a href =“ https://www.w3schools.com”>访问W3Schools< / a>

预期结果:

测试链接< a href =“ https://www.w3schools.com”>访问W3Schools< / a>

我对jsoup的尝试:

public String cleanHtml(String html)
    {
        Whitelist whitelist = Whitelist.none();
        whitelist.addTags("a");

        return Jsoup.clean(html, whitelist);
    }

结果是:

测试链接< a>访问W3Schools< / a>

如何删除所有标签,但保留整个href?

解决方法:

您需要使用addAttributes.在这里,您传递允许的属性列表,whitelist.addAttributes(“ a”,“ href”,“ id”,“ more”);

试试这个:

String html = "<b>Test link</b> <a href=\"https://www.w3schools.com\">Visit W3Schools</a>";
    Whitelist whitelist = Whitelist.none();
    whitelist.addTags("a");
    whitelist.addAttributes("a","href");

    System.out.println(Jsoup.clean(html, whitelist));

标签:strip-tags,jsoup,java,android
来源: https://codeday.me/bug/20191025/1925575.html