Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 10 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
我不了解你,但我认为如果你试图操作的html页面不是很复杂,那么你可以像这样自己构建它:
这样你就可以完全控制html,并且加载速度比从外部 html 加载它。
I don't know about you but I think if the html page you are trying to manipulate isn't very complicated then you can built it yourself like that:
That way you can have full control over the html and it is faster to load than loading it from a outer html.
HTMLEditorKit
不是一个 HTML 编辑器,而是一个文档模型编辑器,它允许将这些文档模型与 HTML 相互转换。编辑器工具包的内部模型不是“HTML”,而是基于DefaultStyledDocument
。让您感到困惑的是有一个HTMLDocument
类。但这只是DefaultStyledDocument
的一个薄包装,因此可以从 HTML 创建它并将其另存为 HTML。您需要的是一个 HTML 解析器。尝试 jTidy。它将读取 HTML,构建一个内部模型(保留
等
HTMLEditorKit
将忽略的内容)。然后,您可以使用 DOM API 来修改模型。也就是说,对于许多用例来说,使用正则表达式或简单的字符串搜索和替换来过滤 HTML 就足够了。
The
HTMLEditorKit
is not an HTML editor but an editor for document models which allows to convert these document models from and to HTML. The internal model of the editor kit is not "HTML" but is based onDefaultStyledDocument
. What confuses you is that there is aHTMLDocument
class. But that is just a thin wrapper for theDefaultStyledDocument
so it can be created from HTML and saved as HTML.What you need is an HTML parser. Try jTidy. It will read the HTML, build an internal model (keeping things like
<script>
whichHTMLEditorKit
will ignore). You can then use a DOM API to modify the model.That said, for many use cases, it's enough to filter the HTML with regular expressions or simple string search&replace.
我不知道是否有任何关于使用
HTMLDocument
和HTMLEditorKit
在 Java 中编辑 HTML 文档的教程。 JDK 的实现有些限制,但它在内部创建了一个类似于 DOM 的元素树。您可以使用 getRootElements() 方法:这里
doc
是HTMLDocument
的实例。我认为使用HTMLDocument
编辑 HTML 并不容易,但这是可能的,请参阅以下方法:insertAfterEnd(Element elem, String htmlText)
insertAfterStart(Element elem, String htmlText)
insertBeforeEnd(Element elem, String htmlText)
insertBeforeStart(Element elem, String htmlText)
setInnerHTML(Element elem, String htmlText)
setOuterHTML(Element elem, String htmlText)
所有这些方法都接受
Element
作为进行编辑的参考点。您可以使用其方法遍历元素的树结构,我向您展示了如何获取对树根的引用。使用这些方法您可以编写可视化 HTML 编辑器。只是为了显示您的 HTML 模型,请调用
JEditorPane
对象。有关如何使用
HTMLDocument
模型操作加载到JEditorPane
中的 HTML 内容的非常简单的示例,请参阅 我的示例应用程序在另一个与 HTML 相关的问题问题,特别是propertyChange
甚至处理程序的代码。尽管为了对 HTML 有更多的控制,我还是建议使用一个创建 HTML DOM 并允许修改它的库。
I don't if there exists any tutorial on using
HTMLDocument
andHTMLEditorKit
for editing HTML documents in Java. The JDK implementation is somewhat limited, yet internally it creates a tree of elements similar to DOM. You can access the tree from HTMLDocument using getRootElements() method:Here
doc
is an instance ofHTMLDocument
. I don't think it is easy to edit HTML withHTMLDocument
but it is possible, see the following methods:insertAfterEnd(Element elem, String htmlText)
insertAfterStart(Element elem, String htmlText)
insertBeforeEnd(Element elem, String htmlText)
insertBeforeStart(Element elem, String htmlText)
setInnerHTML(Element elem, String htmlText)
setOuterHTML(Element elem, String htmlText)
All of these methods accept
Element
as a reference point where the editing takes place. You can walk the tree structure of elements using its methods, and I showed you how to get the reference to the root of the tree.Using these methods you can write a visual HTML editor. Just to show your HTML model, call
setEditable(false)
method onJEditorPane
object.For a very simple example on how you can manipulate the contents of HTML loaded into
JEditorPane
withHTMLDocument
model, see my sample application in the answer to another HTML-related question, in particular the code ofpropertyChange
even handler.Although to have more control on the HTML, I would recommend using a library which creates HTML DOM and allows to modify it.