gpt4 book ai didi

android - JSOUP : Retain specific tags

转载 作者:行者123 更新时间:2023-11-30 01:38:54 24 4
gpt4 key购买 nike

我们可以保留特定标签并从节点中删除其余标签的任何方式。喜欢

<li>my name is <a>this is link</a>
<li>again bullet</li>
</li>

在这里我想删除嵌套的 li 标签并只保留一个标签。

我将如何使用 jsoup/来完成它。

最佳答案

Select the tags you want to remove and ask Jsoup to remove them.

根据您帖子中的示例,下面的短语是如何翻译的:

String html = "<li>my name is <a>this is link</a>  <li>again bullet</li></li>";
Document doc = Jsoup.parse(html);

System.out.println("\nBEFORE:\n" + doc);

doc.select("li ~ li").remove();

System.out.println("\nAFTER:\n" + doc);

请注意,原来的html代码是无效的。这就是 Jsoup 移出嵌套的 li 标签的原因。

输出

BEFORE:
<html>
<head></head>
<body>
<li>my name is <a>this is link</a> </li>
<li>again bullet</li>
</body>
</html>

AFTER:
<html>
<head></head>
<body>
<li>my name is <a>this is link</a> </li>
</body>
</html>

关于android - JSOUP : Retain specific tags,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34775062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com