Jsoup – 提取文本

我需要从这样的节点中提取文本:

Some text with tags might go here.

Also there are paragraphs

More text can go without paragraphs

我需要建立:

 Some text with tags might go here. Also there are paragraphs More text can go without paragraphs 

Element.text只返回div的所有内容。 Element.ownText – 不在children元素中的所有内容。 两者都错了。 通过children迭代忽略文本节点。

是否有方法迭代元素的内容以接收文本节点。 例如

  • 文本节点 – 一些文本
  • 节点 – 带标签
  • 文本节点 – 可能会在这里。
  • 节点

    – 还有段落

  • 文本节点 – 更多文本可以没有段落
  • 节点

Element.children()返回一个Elements对象 – 一个Element对象列表。 查看父类Node ,您将看到允许您访问任意节点的方法,而不仅仅是Elements,例如Node.childNodes() 。

 public static void main(String[] args) throws IOException { String str = "
" + " Some text with tags might go here." + "

Also there are paragraphs

" + " More text can go without paragraphs
" + "
"; Document doc = Jsoup.parse(str); Element div = doc.select("div").first(); int i = 0; for (Node node : div.childNodes()) { i++; System.out.println(String.format("%d %s %s", i, node.getClass().getSimpleName(), node.toString())); } }

结果:

 1个TextNode 
 一些文字 
 2元素带标签
 3 TextNode可能会在这里。 
 4元素

还有段落 5 TextNode更多文本可以没有段落 6个元素

 for (Element el : doc.select("body").select("*")) { for (TextNode node : el.textNodes()) { node.text() )); } } 

假设您只想要文本(没有标签)我的解决方案如下。
输出是:
一些带标签的文字可能会在这里。 还有段落。 更多文字可以没有段落

 public static void main(String[] args) throws IOException { String str = "
" + " Some text with tags might go here." + "

Also there are paragraphs.

" + " More text can go without paragraphs
" + "
"; Document doc = Jsoup.parse(str); Element div = doc.select("div").first(); StringBuilder builder = new StringBuilder(); stripTags(builder, div.childNodes()); System.out.println("Text without tags: " + builder.toString()); } /** * Strip tags from a List of type
Node * @param builder StringBuilder : input and output * @param nodesList List of type Node */ public static void stripTags (StringBuilder builder, List nodesList) { for (Node node : nodesList) { String nodeName = node.nodeName(); if (nodeName.equalsIgnoreCase("#text")) { builder.append(node.toString()); } else { // recurse stripTags(builder, node.childNodes()); } } }

您可以将TextNode用于此目的:

 List bodyTextNode = doc.getElementById("content").textNodes(); String html = ""; for(TextNode txNode:bodyTextNode){ html+=txNode.text(); }