2012-04-16 20 views
7

ayıklanması Böyle bir düğümden metni ayıklamak gerekir:Jsoup - Metin

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

Ve inşa etmek gerekir: div

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

Element.text döner sadece tüm içeriği. Element.ownText - çocuk öğelerinin içinde olmayan her şey. Her ikisi de yanlış. children ile yinelemek, metin düğümlerini yok sayar.

yanı metin düğümlerini almak için bir öğenin içeriğini yineleme yolu vardır mı. Örneğin.

  • Metin düğümü - Bazı metin
  • Düğüm < b> - etiketleriyle
  • Metin düğümü - burada gidebilir.
  • Düğüm < p> - Ayrıca paragraflar vardır
  • Metin düğümü - Daha metin paragrafları olmadan gidebilir
  • Düğüm < br> - listesini ->

cevap

11

Element.children() bir Elements nesnesi döndüren < boş Element nesneler. Ebeveyn sınıfına bakarak, Node, yalnızca Node.childNodes() gibi Elements öğelerini değil, rasgele düğümlere erişim sağlama yöntemleri görürsünüz.

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

Sonuç:

 
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

Çalışır mükemmel, teşekkürler! –

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

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

        node.text())); 

     } 

    } 
1

sen benim çözüm altındadır yalnızca (etiket) metin istiyorum varsayarsak.
Çıktı:
Etiketli bazı metinler buraya gidebilir. Ayrıca paragraflar da var. Daha metni bu amaçla TextNode kullanabilirsiniz paragraflarda

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

olmadan gidebilir:

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