首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何阻止HTMLWriter编写糟糕的HTML?(使用HTMLEditorKit)

如何阻止HTMLWriter编写糟糕的HTML?(使用HTMLEditorKit)
EN

Stack Overflow用户
提问于 2012-07-19 09:41:08
回答 2查看 700关注 0票数 1

这是令人惊讶的行为。我创建一个JTextPane,将其设置为使用HTMLEditorKit,并使用有效的HTMLEditorKit填充它。但是默认情况下,Java的HTMLWriter会创建无效的 HTML。大多数项目都是正确序列化的,但是img标记失去了它们的结束斜线,因此:

代码语言:javascript
复制
<img src="https://localhost:9443/ccm/service/com.ibm.team.workitem.common.internal.model.IImageContentService/processattachment/_7rfpIMXdEeGLRroh_7O2yQ/workflow/resolve.gif" alt="Resolved" border="0"/>

写成:

代码语言:javascript
复制
<img src="https://localhost:9443/ccm/service/com.ibm.team.workitem.common.internal.model.IImageContentService/processattachment/_7rfpIMXdEeGLRroh_7O2yQ/workflow/resolve.gif" alt="Resolved" border="0">

我对每件事都使用默认值。为什么它不起作用,有什么简单的解决办法吗?

下面是一个代码片段:

代码语言:javascript
复制
    JTextPane editor = new JTextPane();
    HTMLEditorKit htmlKit = new HTMLEditorKit();
    editor.setContentType("text/html");
    editor.setEditorKit(htmlKit);   
    editor.setText( '*<ADD SOME VALID HTML FROM A FILE>*'  );       
    HTMLDocument d = (HTMLDocument)editor.getDocument();
    StringWriter fw = new StringWriter();
    HTMLWriter aHTMLWriter = new HTMLWriter(fw,d);
    aHTMLWriter.write();
    String txt = fw.toString();
    //  Now  txt is not valid HTML ... eek!
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-19 09:58:01

实际上,它是有效的HTML,但它不是有效的XHTML。据我所知,不可能将它输出到XHTML。您可以使用正则表达式对输出进行后置处理,也可以像Freeplane编写XHTMLWriter时那样扩展XHTMLWriter

票数 0
EN

Stack Overflow用户

发布于 2012-07-19 09:50:17

遗憾的是,HTMLEditorKit只支持HTML3.2,因此不应该关闭img标记。所以它的行为是“正确的”。

增强请求是1999年发布的,所以可能很快就会实施。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11558033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档