How do I load an HTML file synchronously into an HTMLDocument?

John Zukowski

// Convert urlString into a BufferedReader
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);

// Create empty HTMLDocument to read into
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument)htmlKit.createDefaultDocument();
// Create parser (javax.swing.text.html.parser.ParserDelegator)
HTMLEditorKit.Parser parser = new ParserDelegator();
// Get parser callback from document
HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
// Load it (true means to ignore character set)
parser.parse(br, callback, true);
About | Sitemap | Contact