How do I convert a document made in Jsoup (the Java html parser) into a string

扶醉桌前 提交于 2019-12-09 05:03:03

问题


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.


回答1:


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




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/6865090/how-do-i-convert-a-document-made-in-jsoup-the-java-html-parser-into-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!