这是令人惊讶的行为。我创建了一个 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!