문서에서 나는 새로운 줄을 보존하는 텍스트를 반환하는 방법이 있다고 생각하지 않습니다. 인쇄 할 텍스트 노드를 식별하면 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
}
}
}
보이는 당신의 예를 들어 (그것은 단지 하나의 텍스트 노드가 있습니다). 당신은 아마도 java.net api를 사용하여이 txt 파일 내용을 얻을 수 있습니다. – user1455836
나는 txt를 희망하는 것으로 바꾸 었습니다. 전에 html로 많은 테스트를했습니다. – BestSiteEditor
분명히 jsoup는 콘텐츠 유형을 추측하기 위해 리소스 이름에 의존하지 않습니다. – user1455836