Java 如何阻止HTMLWriter编写糟糕的HTML?(使用HTMLeditor工具包)

Java 如何阻止HTMLWriter编写糟糕的HTML?(使用HTMLeditor工具包),java,htmleditorkit,htmlwriter,Java,Htmleditorkit,Htmlwriter,这是令人惊讶的行为。我创建了一个JTextPane,将其设置为使用HTMLEditorKit,并用有效的HTML填充它。但是默认情况下,Java的HTMLWriter会创建无效的HTML。大多数项目都已正确序列化,但img标记会丢失其结束斜杠,因此: <img src="https://localhost:9443/ccm/service/com.ibm.team.workitem.common.internal.model.IImageContentService/processatta

这是令人惊讶的行为。我创建了一个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!
JTextPane编辑器=新建JTextPane();
HTMLEditorKit htmlKit=新的HTMLEditorKit();
setContentType(“text/html”);
setEditorKit(htmlKit);
editor.setText('**');
HTMLDocument d=(HTMLDocument)editor.getDocument();
StringWriter fw=新的StringWriter();
HTMLWriter aHTMLWriter=新HTMLWriter(fw,d);
aHTMLWriter.write();
字符串txt=fw.toString();
//现在txt不是有效的HTML。。。哎呀!

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


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

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


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

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

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

谢谢您的回答。是的,我完全忘记了历史性的HTML3.2是如何工作的(过去时)。“增强请求”的历史让我读起来非常悲伤。谢谢你的回答。是的,我完全忘记了历史性的HTML3.2是如何工作的(过去时)。“增强请求”的历史让我读起来非常悲伤。