I’m working on a method that takes a String of HTML and returns an analogous
javax.swing.text.html.HTMLDocument
What is the most efficient way of doing this?
The way I’m currently doing this is to use a SAX parser to parse the HTML string. I keep track of when I hit open tags (for example, <i>). When I hit the corresponding close tag (for example, </i>), I apply the italics style to the characters I’ve hit in between.
This certainly works, but it’s not fast enough. Is there a faster way of doing this?
Try to use
HtmlEditorKitclass. It supports parsing of HTML content that can be read directly fromString(e.g. throughStringReader). There seems to be an article about how to do this.Edit: To give an example, basically I think it could be done like this (aftrer the code is executed,
htmlDocshould contain the loaded document…):