[英]JSoup- Getting text between tags as is
Scenario: I used Apache Tika
to get XHTML
from a DOCX
file. 场景:我使用
Apache Tika
从DOCX
文件中获取XHTML
。 I need to parse this XHTML
to get text between particular tags (eg div or p tag). 我需要解析这个
XHTML
来获取特定标签之间的文本(例如div或p标签)。 For this I use Jsoup
to get text between tags. 为此,我使用
Jsoup
在标签之间获取文本。
Problem: Originally the XHTML
has this text: 问题:最初
XHTML
有这样的文字:
some text [tab-space][tab-space] other text.
But with Jsoup
i am getting this: 但是对于
Jsoup
我得到了这个:
some text other text.
So the tag spaces are missing but i need to get the text as is ie including tag-spaces
. 所以标签空间丢失但我需要按原样获取文本,即包括
tag-spaces
。 Is it possible to do this using Jsoup
or is there any other Java
library to do so? 是否可以使用
Jsoup
执行此操作,还是有任何其他Java
库可以执行此操作?
Use the getWholeText method for TextNodes: https://jsoup.org/apidocs/org/jsoup/nodes/TextNode.html#getWholeText-- 对TextNodes使用getWholeText方法: https ://jsoup.org/apidocs/org/jsoup/nodes/TextNode.html#getWholeText--
final Document doc = Jsoup.parse(new File(".\\source.xhtml"), "UTF-8");
for (Element result : doc.select("div")) {
final String text = ((TextNode) result.childNode(0)).getWholeText();
System.out.println(text);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.