如何处理 Java HTMLDocument 中的自定义标签?
我有一个 swing 客户端,它通过使用 JEditorPane 和 HTMLDocument 类来呈现 HTML。
我遇到的问题是,我希望能够将文档的一个区域定义为稍后将检索的某些文本的容器,然后使用 document.setInnerHTML(element, data); 将占位符内容替换为“真实”内容。
因此,我决定定义一个与此类似的标签:
<html>
<body>
some text <mytag id="1>placeholder</mytag> some more text
</body>
</html>
当我使用 document.getElement(String id)
时出现问题 - 它发现 mytag 作为一个元素,但它认为它是一个叶元素没有内容,所以我无法对其调用 .setInner() 。 查看它的父元素列表,它认为有 3 个 - 我的标签刚刚被解释为单独的组件:
mytag (start)
内容
mytag (end)
所以,我猜我需要告诉文档(或其解析器)我的标签,但这就是我失败的地方,因为我对这个领域相当陌生。
那么,有人有这方面的经验吗? 我可以作弊并使用跨度标签,但这感觉不对,因为(为简洁起见排除)我还需要针对该标签存储一个附加属性。
在此先感谢您的时间...
I have a swing client that renders HTML through the use of the JEditorPane
and HTMLDocument
classes.
The problem I have is that I want to be able to define an area of the document as a container for some text that will be retrieved at a later date, and then use document.setInnerHTML(element, data);
to swap out the place-holder content with the "real" content.
So, I decided that I would define a tag similar to this:
<html>
<body>
some text <mytag id="1>placeholder</mytag> some more text
</body>
</html>
The problem occurs when I use document.getElement(String id)
- it finds mytag as an element, but it thinks it's a leaf element that has no content and so I cannot call .setInner() on it. Looking at it's parents list of elements, it thinks there are 3 - my tag has just been interpreted as individual components:
mytag (start)
content
mytag (end)
So, I'm guessing that I need to tell the document (or it's parser) about my tag, but that's where I'm falling flat as I'm fairly new to this area.
So, has anyone had any experience with this at all? I could cheat and use a span tag, but that doesn't feel right as (excluded for brevity) I also need to store an additional attribute against the tag.
Thanks in advance for your time...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您需要重写 HTMLDocument 以返回识别自定义标签的阅读器。 请参阅 HTMLDocument# getReader 了解更多信息。 您需要返回 HTMLReader 理解自定义标签(通过 registerTag 方法)。
It looks like you need to override the HTMLDocument to return a reader that recognises the custom tags. See HTMLDocument#getReader for more information. You'll need to return a subclass of HTMLReader which understands the custom tag (via the registerTag method).
也许将您的详细信息设置为标签的属性?
perhaps set your detail as an attribute of your tag?
http://java-sl.com/custom_tag_html_kit.html
这展示了如何添加自定义标签。 您可以使用它来编写自己的标签支持。
http://java-sl.com/custom_tag_html_kit.html
That shows how to add a custom tag. You can use this to write your own tags support.