从HTML字符串(在Java中)创建HTMLDocument [英] Creating an HTMLDocument from a String of HTML (in Java)
问题描述
javax.swing.text。 html.HTMLDocument
最有效的方法是什么?
我目前正在做的是使用SAX解析器来解析HTML字符串。我跟踪打开标签的时间(例如,< i>)。当我点击相应的关闭标签(例如,< / i>)时,我将斜体字样式应用于我之间的字符。
工作,但它还不够快。有更快的方法吗?
尝试使用 HtmlEditorKit
类。它支持解析可直接从 String
(例如通过 StringReader
)读取的HTML内容。 似乎有一篇关于如何做到这一点的文章。
编辑:给出一个例子,基本上我认为可以这样做(执行代码执行的代码 htmlDoc
应该包含加载的文档...):
Reader stringReader = new StringReader(string);
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc =(HTMLDocument)htmlKit.createDefaultDocument();
HTMLEditorKit.Parser parser = new ParserDelegator();
parser.parse(stringReader,htmlDoc.getReader(0),true);
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 HtmlEditorKit
class. It supports parsing of HTML content that can be read directly from String
(e.g. through StringReader
). 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, htmlDoc
should contain the loaded document...):
Reader stringReader = new StringReader(string);
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
HTMLEditorKit.Parser parser = new ParserDelegator();
parser.parse(stringReader, htmlDoc.getReader(0), true);
这篇关于从HTML字符串(在Java中)创建HTMLDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!