0

在我的 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 或类)

4

1 回答 1

3

您可以使用 XML 解析库。

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

请注意,这是伪的。

newText现在将是-text或节点文本是什么。

编辑:就“文本可以包含 html 元素” 而言,您的问题有点模棱两可。
如果它不包含 html 标签,那么您不能使用 XML 解析器,这会带来问题.. 如果它包含标签,那么您为什么不能这样做...

String newString = "-" + a;
于 2013-04-24T14:48:21.117 回答