2012-07-19 26 views
1

這是令人驚訝的行爲。我創建了一個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! 

回答

0

實際上,它是有效的HTML,但它不是有效的XHTML。據我所知,不可能讓它輸出XHTML。您可以使用正則表達式來後處理輸出,或者像Freeplane那樣在寫XHTMLWriter時擴展HTMLWriter。

1

可悲的是一個HTMLEditorKit只支持HTML 3.2,而對於該的img標籤不應該被關閉。所以它的行爲「正確」。

A Request for enhancement於1999年發佈,因此可能很快就會實施。

+0

謝謝你的答案。是的,我會清除忘記歷史HTML 3.2的工作原理(過去式)。「請求增強」的歷史讓人很難過。 。 。 – 2012-07-19 19:47:58

相關問題