2012-04-16 43 views
7

tôi cần phải trích xuất văn bản từ một nút như thế này:Jsoup - trích văn bản

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

Và tôi cần phải xây dựng:

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

Element.text lợi nhuận chỉ là tất cả nội dung của div. Element.ownText - mọi thứ không nằm trong các phần tử con. Cả hai đều sai. Lặp lại thông qua children bỏ qua các nút văn bản.

Có cách nào để lặp nội dung của phần tử để nhận các nút văn bản. Ví dụ.

  • Tiêu nút - Một số văn bản
  • Node < b> - với thẻ
  • Tiêu nút - có thể đi đây.
  • Node < p> - Cũng có những đoạn
  • Tiêu nút - Thêm văn bản có thể đi mà không có đoạn
  • Node < br> - < trống>

Trả lời

11

Element.children() trả về một đối tượng Elements - một danh sách các Element đối tượng. Nhìn vào lớp cha, Node, bạn sẽ thấy các phương pháp để cung cấp cho bạn quyền truy cập vào các nút tùy ý, không chỉ các phần tử, chẳng hạn như Node.childNodes().

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(); 
    int i = 0; 

    for (Node node : div.childNodes()) { 
     i++; 
     System.out.println(String.format("%d %s %s", 
       i, 
       node.getClass().getSimpleName(), 
       node.toString())); 
    } 
} 

Kết quả:

 
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/> 
+0

trình một cách hoàn hảo, cảm ơn! –

3
for (Element el : doc.select("body").select("*")) { 

     for (TextNode node : el.textNodes()) { 

        node.text())); 

     } 

    } 
1

Giả sử bạn muốn nhắn tin cho chỉ (không có thẻ) giải pháp của tôi là dưới đây.
Đầu ra là:
Một số văn bản có thẻ có thể đến đây. Ngoài ra còn có các đoạn văn. Nhiều văn bản có thể đi mà không có đoạn

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()); 
     } 
    } 
} 
1

bạn có thể sử dụng TextNode cho mục đích này:

List<TextNode> bodyTextNode = doc.getElementById("content").textNodes(); 
    String html = ""; 
    for(TextNode txNode:bodyTextNode){ 
     html+=txNode.text(); 
    } 
Các vấn đề liên quan