Jsoup 文本节点被其他标签一分为二
Jsoup text node split in two by other tag
我有这样的东西
<div>
textA
<table>
<tbody>
<tr>
<td>textB</td>
</tr>
</tbody>
</table>
textC
</div>
我想拆分成这样:
textA textB // this is my first string
textC // this is my second string
有没有办法拆分 textA 和 textC,即使它们属于同一节点?
Is there any way to split textA and textC even if they are part of the same node?
你可以做的是用 Jsoup 加载 HTML 代码,然后让它给出整个文本。之后,您可以将此文本拆分为 String#split
.
示例代码
String html = "<div>\n textA\n <table>\n <tbody>\n <tr>\n <td>textB</td>\n </tr>\n </tbody>\n </table>\n textC\n</div>";
Document doc = Jsoup.parse(html);
Element div =doc.select("div").first();
if (div!=null) {
String[] splittedParts = div.text().split("(?=textC)");
for(String s:splittedParts) {
System.out.println(s);
}
}
输出
textA textB
textC
我有这样的东西
<div>
textA
<table>
<tbody>
<tr>
<td>textB</td>
</tr>
</tbody>
</table>
textC
</div>
我想拆分成这样:
textA textB // this is my first string
textC // this is my second string
有没有办法拆分 textA 和 textC,即使它们属于同一节点?
Is there any way to split textA and textC even if they are part of the same node?
你可以做的是用 Jsoup 加载 HTML 代码,然后让它给出整个文本。之后,您可以将此文本拆分为 String#split
.
示例代码
String html = "<div>\n textA\n <table>\n <tbody>\n <tr>\n <td>textB</td>\n </tr>\n </tbody>\n </table>\n textC\n</div>";
Document doc = Jsoup.parse(html);
Element div =doc.select("div").first();
if (div!=null) {
String[] splittedParts = div.text().split("(?=textC)");
for(String s:splittedParts) {
System.out.println(s);
}
}
输出
textA textB
textC