从HTMLDocument获取所有html作为String [英] get all html as a String from HTMLDocument

查看:594
本文介绍了从HTMLDocument获取所有html作为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中编码..

Im coding in Java..

有谁知道如何将javax.swing.text.html.HTMLDocument的内容作为字符串获取?这就是我到目前为止...

Does anyone know how i can get the content of a javax.swing.text.html.HTMLDocument as a String? This is what i´ve got so far...

URL url = new URL( "http://www.test.com" );

HTMLEditorKit kit = new HTMLEditorKit(); 
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument(); 
doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
Reader HTMLReader = new InputStreamReader(url.openConnection().getInputStream()); 
kit.read(HTMLReader, doc, 0); 

我需要将HTMLDocument的内容作为字符串。

I need the content of the HTMLDocument as a String.

示例:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">    <html><head><meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">

......等等。

任何帮助将不胜感激。我需要使用HTMLDocument类才能正确处理html:)

Any help would be appreciated. I need to use HTMLDocument class in order for the html to be processed correctly :)

谢谢Daniel

推荐答案

StringWriter writer = new StringWriter();
kit.write(writer, doc, 0, doc.getLength());
String s = writer.toString();

这篇关于从HTMLDocument获取所有html作为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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