在哪里可以找到好的 HTMLEditorKit 教程/参考,它实际上解释了如何编辑 HTML 文档?

发布于 2024-08-06 04:21:06 字数 1536 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

高冷爸爸 2024-08-13 04:21:06

我不了解你,但我认为如果你试图操作的html页面不是很复杂,那么你可以像这样自己构建它:

HTMLDocument doc = new HTMLDocument();

HTMLEditorKit kit = new HTMLEditorKit();

jEditorPane.setDocument(doc);

jEditorPane.setEditorKit(kit);

kit.insertHTML(doc, doc.getLength(), "<label> This label will be inserted inside the body  directly </label>", 0, 0, null);
kit.insertHTML(doc, doc.getLength(), "<br/>", 0, 0, null);
kit.insertHTML(doc, doc.getLength(), putYourVariableHere, 0, 0, null);

这样你就可以完全控制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:

HTMLDocument doc = new HTMLDocument();

HTMLEditorKit kit = new HTMLEditorKit();

jEditorPane.setDocument(doc);

jEditorPane.setEditorKit(kit);

kit.insertHTML(doc, doc.getLength(), "<label> This label will be inserted inside the body  directly </label>", 0, 0, null);
kit.insertHTML(doc, doc.getLength(), "<br/>", 0, 0, null);
kit.insertHTML(doc, doc.getLength(), putYourVariableHere, 0, 0, null);

That way you can have full control over the html and it is faster to load than loading it from a outer html.

魂ガ小子 2024-08-13 04:21:06

HTMLEditorKit 不是一个 HTML 编辑器,而是一个文档模型编辑器,它允许将这些文档模型与 HTML 相互转换。编辑器工具包的内部模型不是“HTML”,而是基于 DefaultStyledDocument。让您感到困惑的是有一个 HTMLDocument 类。但这只是 DefaultStyledDocument 的一个薄包装,因此可以从 HTML 创建它并将其另存为 HTML。

您需要的是一个 HTML 解析器。尝试 jTidy。它将读取 HTML,构建一个内部模型(保留

也就是说,对于许多用例来说,使用正则表达式或简单的字符串搜索和替换来过滤 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 on DefaultStyledDocument. What confuses you is that there is a HTMLDocument class. But that is just a thin wrapper for the DefaultStyledDocument 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> which HTMLEditorKit 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.

吃素的狼 2024-08-13 04:21:06

我不知道是否有任何关于使用 HTMLDocumentHTMLEditorKit 在 Java 中编辑 HTML 文档的教程。 JDK 的实现有些限制,但它在内部创建了一个类似于 DOM 的元素树。您可以使用 getRootElements() 方法:

Element html = doc.getRootElements()[0];

这里 docHTMLDocument 的实例。我认为使用 HTMLDocument 编辑 HTML 并不容易,但这是可能的,请参阅以下方法:

所有这些方法都接受 Element 作为进行编辑的参考点。您可以使用其方法遍历元素的树结构,我向您展示了如何获取对树根的引用。

使用这些方法您可以编写可视化 HTML 编辑器。只是为了显示您的 HTML 模型,请调用 JEditorPane 对象。

有关如何使用 HTMLDocument 模型操作加载到 JEditorPane 中的 HTML 内容的非常简单的示例,请参阅 我的示例应用程序另一个与 HTML 相关的问题问题,特别是propertyChange甚至处理程序的代码。

尽管为了对 HTML 有更多的控制,我还是建议使用一个创建 HTML DOM 并允许修改它的库。

I don't if there exists any tutorial on using HTMLDocument and HTMLEditorKit 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:

Element html = doc.getRootElements()[0];

Here doc is an instance of HTMLDocument. I don't think it is easy to edit HTML with HTMLDocument but it is possible, see the following methods:

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 on JEditorPane object.

For a very simple example on how you can manipulate the contents of HTML loaded into JEditorPane with HTMLDocument model, see my sample application in the answer to another HTML-related question, in particular the code of propertyChange 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文