PeerScope now shows all by default! jHome | jGurus | jFAQ | jLearn | jCode | jNeighbors | jNews






Personalize
Edit Account
Member Page
Log Out



Wish List
Features
Contact
About
Privacy Policy
Copyright
Legal Notices



jGuru FAQ Entry 4370

Question How do I load an HTML file synchronously into an HTMLDocument?
Topics Java:API:Swing:HTMLEditorKit
Author John Zukowski
Links Printable entry, FAQ Home, All entries by John Zukowski
Created 2000-01-08 22:58:28.926
Modified 2000-01-09 05:46:00.317

Answer

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

Submit FAQ Entry Feedback
Praise, correct, augment, or generally comment on this FAQ entry. Your feedback will appear attached to this entry after a human has reviewed the content and will be forwarded to the author(s). Your input can be plain text or contain HTML body tags.