Java Jsoup-提取文本

Java Jsoup-提取文本,java,iteration,jsoup,text-extraction,Java,Iteration,Jsoup,Text Extraction,我需要从如下节点中提取文本: <div> Some text <b>with tags</b> might go here. <p>Also there are paragraphs</p> More text can go without paragraphs<br/> </div> 一些带有标记的文本可能会出现在这里。 还有一些段落 更多文本可以不带段落 我需要建立: Some

我需要从如下节点中提取文本:

<div>
    Some text <b>with tags</b> might go here.
    <p>Also there are paragraphs</p>
    More text can go without paragraphs<br/>
</div>

一些带有标记的文本可能会出现在这里。
还有一些段落

更多文本可以不带段落
我需要建立:

Some text <b>with tags</b> might go here.
Also there are paragraphs
More text can go without paragraphs
一些带有标记的文本可能会出现在这里。
还有一些段落
更多文本可以不使用段落
Element.text
仅返回div的所有内容。
Element.ownText
-不在子元素内的所有内容。两者都错了。迭代子节点会忽略文本节点

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

  • 文本节点-一些文本
  • 节点-带标记
  • 文本节点-可能位于此处
  • 节点-也有段落
  • 文本节点-更多文本可以不包含段落
  • 节点
    -
返回一个对象-对象列表。查看父类,您将看到允许您访问任意节点的方法,而不仅仅是元素,例如

publicstaticvoidmain(字符串[]args)引发IOException{
字符串str=“”+
“某些带有标记的文本可能会出现在此处。”+
“还有段落

”+ “更多文本可以不包含段落
”+ ""; Document doc=Jsoup.parse(str); Element div=doc.select(“div”).first(); int i=0; 对于(节点:div.childNodes()){ i++; System.out.println(String.format(“%d%s%s”), 我 node.getClass().getSimpleName(), node.toString()); } }
结果:

1 TextNode Some text 2 Element <b>with tags</b> 3 TextNode might go here. 4 Element <p>Also there are paragraphs</p> 5 TextNode More text can go without paragraphs 6 Element <br/> 1文本节点 一些文本 2带标记的元素 3 TextNode可能在这里。 4元素也有段落

5文本节点更多文本可以不包含段落 6元素

假设您只需要文本(无标签),我的解决方案如下。
输出为:
一些带有标记的文本可能会出现在这里。还有一些段落。更多文本可以不带段落

public static void main(String[] args) throws IOException {
    String str = 
                "<div>"  
            +   "    Some text <b>with tags</b> might go here."
            +   "    <p>Also there are paragraphs.</p>"
            +   "    More text can go without paragraphs<br/>" 
            +   "</div>";

    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 <code>Node</code>
 * @param builder StringBuilder : input and output
 * @param nodesList List of type <code>Node</code>
 */
public static void stripTags (StringBuilder builder, List<Node> 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<TextNode> bodyTextNode = doc.getElementById("content").textNodes();
    String html = "";
    for(TextNode txNode:bodyTextNode){
        html+=txNode.text();
    }
List bodyTextNode=doc.getElementById(“内容”).textNodes();
字符串html=“”;
用于(TextNode txNode:bodyTextNode){
html+=txNode.text();
}

我猜您在循环中缺少system.out.println,但这是一个可以递归提取所有文本节点的示例。
public static void main(String[] args) throws IOException {
    String str = 
                "<div>"  
            +   "    Some text <b>with tags</b> might go here."
            +   "    <p>Also there are paragraphs.</p>"
            +   "    More text can go without paragraphs<br/>" 
            +   "</div>";

    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 <code>Node</code>
 * @param builder StringBuilder : input and output
 * @param nodesList List of type <code>Node</code>
 */
public static void stripTags (StringBuilder builder, List<Node> nodesList) {

    for (Node node : nodesList) {
        String nodeName  = node.nodeName();

        if (nodeName.equalsIgnoreCase("#text")) {
            builder.append(node.toString());
        } else {
            // recurse
            stripTags(builder, node.childNodes());
        }
    }
}
List<TextNode> bodyTextNode = doc.getElementById("content").textNodes();
    String html = "";
    for(TextNode txNode:bodyTextNode){
        html+=txNode.text();
    }