如何将Jsoup(Java html解析器)中生成的文档转换为字符串 [英] How do I convert a document made in Jsoup (the Java html parser) into a string

查看:479
本文介绍了如何将Jsoup(Java html解析器)中生成的文档转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jsoup中的文档,看起来像这样

 文档doc = Jsoup.connect(http: //en.wikipedia.org /)得到()。 

如何将 doc 转换为你知道吗?

 文档doc = Jsoup.connect(http://en.wikipedia.org/)。get(); 
String htmlString = doc.toString();

文档扩展元素时,它还具有得到了方法 html(),根据 API 检索元素的内部HTML。这应该工作:

 文档doc = Jsoup.connect(http://en.wikipedia.org/)。得到(); 
String htmlString = doc.html();

其他信息:



每个 Document 对象都有一个对内部类 Document.OutputSettings 的实例的引用,可以通过方法 outputSettings() of Document。在那里,您可以使用setter prettyPrint(true / false)启用/禁用漂亮打印。有关进一步信息,请参阅文档 Document.OutputSettings 的API


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.

解决方案

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

这篇关于如何将Jsoup(Java html解析器)中生成的文档转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆