這是令人驚訝的行爲。我創建了一個JTextPane,將其設置爲使用HTMLEditorKit,並用有效的HTML填充它。但默認情況下,Java的HTMLWriter創建了無效的 HTML。大多數項目都正確序列化了,但IMG標籤失去了結束斜槓這樣:如何阻止HTMLWriter寫入錯誤的HTML? (使用HTMLEditorKit)
<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!
謝謝你的答案。是的,我會清除忘記歷史HTML 3.2的工作原理(過去式)。「請求增強」的歷史讓人很難過。 。 。 – 2012-07-19 19:47:58