gpt4 book ai didi

java - Java中获取非html字符串的一部分

转载 作者:行者123 更新时间:2023-12-01 23:52:02 28 4
gpt4 key购买 nike

在我的 Java 应用程序中,我有必须编辑的字符串。问题是这些字符串可以包含 HTML 标签/元素,这些标签/元素不应该被编辑(没有 id 来检索元素)。

场景(添加-):

String a = "<span> <table> </table>  </span> <div></div> <div> text 2</div>";
should become: <span> <table> </table> </span> <div></div> <div> -text 2</div>

String b = "text";
should become: -text

String c = "<p> t </p>";
should become: <p> -t </p>

我的问题是:如何检索可以包含 html 标签的字符串中的文本(无法添加 id 或 class)

最佳答案

您可以使用 XML 解析库。

String newText = null;
for ( Node node : document.nodes() ) {
if ( node.text() != null ) newText = "-" + node.text();
}

请注意,这是伪的。

newText 现在将是 -text 或任何节点文本。

编辑:您的问题在“文本可以包含 html 元素”方面有点含糊。
如果它不包含 html 标签,那么你就不能使用 XML 解析器,这就提出了问题..如果它包含标签,那么你为什么不能这样做......

String newString = "-" + a;

关于java - Java中获取非html字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16194974/

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