这是令人惊讶的行为。我创建一个JTextPane,将其设置为使用HTMLEditorKit,并使用有效的HTMLEditorKit填充它。但是默认情况下,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!发布于 2012-07-19 09:58:01
实际上,它是有效的HTML,但它不是有效的XHTML。据我所知,不可能将它输出到XHTML。您可以使用正则表达式对输出进行后置处理,也可以像Freeplane编写XHTMLWriter时那样扩展XHTMLWriter。
发布于 2012-07-19 09:50:17
遗憾的是,HTMLEditorKit只支持HTML3.2,因此不应该关闭img标记。所以它的行为是“正确的”。
增强请求是1999年发布的,所以可能很快就会实施。
https://stackoverflow.com/questions/11558033
复制相似问题