28

I have a document that was made in jsoup that looks like this

Document doc = Jsoup.connect("http://en.wikipedia.org/").get();

How do i convert that doc into a string.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Hudson Hughes
  • 343
  • 1
  • 3
  • 9

3 Answers3

43

Have you tried:

Document doc = Jsoup.connect("http://en.wikipedia.org/").get();
String htmlString = doc.toString();

As Document extends Element it also has got the method html() which "Retrieves the element's inner HTML" according to the API. So that should work:

Document doc = Jsoup.connect("http://en.wikipedia.org/").get();
String htmlString = doc.html();

Additional Info:

Each Document object has got a reference to an instance of the inner class Document.OutputSettings which can be accessed via the method outputSettings() of Document. There you can enable/disable pretty-printing by using the setter prettyPrint(true/false). See the API for Document and Document.OutputSettings for furtherinformation

das_weezul
  • 6,082
  • 2
  • 28
  • 33
  • The first code block gave me `[Ljava.lang.String;@383534aa` instead of the html / content. BTW what if it's a `Document[]`? – Hack-R Sep 12 '16 at 00:25
9

doc.toString() works, as does doc.outerHtml().

Jeremy Roman
  • 16,137
  • 1
  • 43
  • 44
0
 Document doc = Jsoup.connect("http://en.wikipedia.org/").get();     
 Elements post = doc.select("div.post-content");
 String dd = post.toString();
 Document ddd = Jsoup.parse(dd);

After parsing the string to document then you can use on it document functions

 Elements scriptTag = ddd.getElementsByTag("script");
 System.out.println(scriptTag);
NomanJaved
  • 1,324
  • 17
  • 32