提取HTML标记之外的文本 [英] Extract text outside of a HTML Tag
本文介绍了提取HTML标记之外的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下的HTML code:
I have the following HTML code:
<div class=example>Text #1</div> "Another Text 1"
<div class=example>Text #2</div> "Another Text 2"
我要提取的标签外的文本,另一个文本1和其他文本2
I want to extract the Text outside the tag, "Another Text 1" and "Another Text 2"
我使用JSoup实现这一目标。
I'm using JSoup to achieve this.
任何想法???
谢谢!
推荐答案
您可以选择下一个节点
(不是元素
!)每个 DIV
-tag的。在你的榜样,他们都是的。
You can select the next Node
(not Element
!) of each div
-tag. In your example they are all TextNode
's.
final String html = "<div class=example>Text #1</div> \"Another Text 1\"\n"
+ "<div class=example>Text #2</div> \"Another Text 2\" ";
Document doc = Jsoup.parse(html);
for( Element element : doc.select("div.example") ) // Select all the div tags
{
TextNode next = (TextNode) element.nextSibling(); // Get the next node of each div as a TextNode
System.out.println(next.text()); // Print the text of the TextNode
}
输出:
"Another Text 1"
"Another Text 2"
这篇关于提取HTML标记之外的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文