2012-04-16 5 views
7

를 추출 나는이 같은 노드에서 텍스트를 추출해야합니다Jsoup는 - 텍스트

<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 반환 단지 모든 컨텐츠를. Element.ownText - 어린이 요소 안에 있지 않은 모든 것. 둘 다 틀리다. children을 반복하면 텍스트 노드가 무시됩니다.

텍스트 노드를 수신하기 위해 요소의 내용을 반복하는 방법이 있습니까? 예 :

  • 텍스트 노드 - 일부 텍스트
  • 노드 < B> - 태그
  • 텍스트 노드 - 여기 있습니다.
  • 노드 < P> - 또한 문단가
  • 텍스트 노드 - 더 많은 텍스트를 단락없이 갈 수
  • 노드 < BR> -의 목록을 ->

답변

11

Element.children()Elements 개체를 반환 < 빈 Element 개체. 상위 클래스 인 Node을 보면 Node.childNodes()과 같이 Element가 아닌 임의의 노드에 액세스 할 수있는 메소드가 표시됩니다.

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

결과 :

 
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

작품을 완벽하게, 감사합니다! –

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

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

        node.text())); 

     } 

    } 
1

당신이 내 솔루션은 아래 만 (아무 태그) 텍스트하지 원하는 가정.
출력 :
태그가있는 텍스트가 여기에 표시 될 수 있습니다. 또한 단락도 있습니다. 더 많은 텍스트는이 목적을 위해 TextNode을 사용할 수 있습니다 단락

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

없이 갈 수 있습니다

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