1

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

<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"/>

写成:

<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">

我对所有东西都使用默认值。为什么它不起作用,有什么简单的解决方法吗?

这是一个代码片段:

    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!
4

2 回答 2

1

遗憾的是 HTMLEditorKit 只支持 HTML 3.2,因此 img 标签不应该被关闭。所以它的行为“正确”。

1999 年发布了增强请求,因此可能很快就会实施。

于 2012-07-19T09:50:17.223 回答
0

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

于 2012-07-19T09:58:01.613 回答