2014-01-16 4 views
3

새 줄을 보존하는 방법이 있습니까 (< BR>가 아닌지). Jsoup?Jsoup로 새 줄을 보존하는 방법은 무엇입니까?

Document pdsc = Jsoup.connect("http://drafts.bestsiteeditor.com/promoters/dsc1387266263.txt").get(); 
String strText = pdsc.body().ownText(); 

tv.setText(strText); 

TXT 파일 콘텐츠는 새로운 줄이있는 양식 텍스트 영역 제출물에 있습니다. 감사합니다. .

+0

보이는 당신의 예를 들어 (그것은 단지 하나의 텍스트 노드가 있습니다). 당신은 아마도 java.net api를 사용하여이 txt 파일 내용을 얻을 수 있습니다. – user1455836

+0

나는 txt를 희망하는 것으로 바꾸 었습니다. 전에 html로 많은 테스트를했습니다. – BestSiteEditor

+0

분명히 jsoup는 콘텐츠 유형을 추측하기 위해 리소스 이름에 의존하지 않습니다. – user1455836

답변

0

문서에서 나는 새로운 줄을 보존하는 텍스트를 반환하는 방법이 있다고 생각하지 않습니다. 인쇄 할 텍스트 노드를 식별하면 getWholeText (http://jsoup.org/apidocs/org/jsoup/nodes/TextNode.html#getWholeText()) 메소드가 있습니다. 모든 html을 원한다면 모든 텍스트 노드를 추출해야합니다 (문서의 재귀 트래버스).

Document pdsc = Jsoup.connect("http://drafts.bestsiteeditor.com/promoters/dsc1387266263.txt").get(); 
    System.out.println(((TextNode) pdsc.select("body").first().childNode(0)).getWholeText()); 

보다 일반적인 솔루션 : jsoup은 HTML 문서에처럼

private static void prinWholeText(Document doc) { 
    List<TextNode> textNode = getAllTextNodes(doc); 
    for(TextNode tn:textNode){ 
     System.out.println(tn.getWholeText()); 
    } 
} 

private static List<TextNode> getAllTextNodes(Document doc) { 
    List<TextNode> nodes = new ArrayList<>(); 
    allTextNodes(doc, nodes); 
    return nodes; 
} 

private static void allTextNodes(Element element, List<TextNode> nodes) { 
    for(Node child: element.childNodes()){ 
     if(child instanceof TextNode){ 
      nodes.add((TextNode) child); 
     } else{ 
      if(child instanceof Element){ 
       allTextNodes((Element) child, nodes); 
      } 
      //implement others 
     } 
    } 
}