2016-09-30 53 views
0

我创建了Java的XML编辑器,我使用JTextPanes显示的XML内容。 的JTextPanes内容类型为"text/html"所以它跳过所有,我希望出现在窗格中XML标签。 这里是大多数JTextPane类:如何打印HTML标签与HTML内容类型的JTextPane在Java中

public class Label extends JTextPane { 
    private static final long serialVersionUID = 6151945111760925061L; 

    public Label(String text) { 
     setContentType("text/html"); 
     setText(StringOperations.toHtml(text)); 
     setEditable(false); 
     setBackground(null); 
     setBorder(null); 
     setFont(new Font("Eras Bold ITC", Font.PLAIN, 11)); 
    } 

} 

这里是转换纯文本到HTML的方法。

public static String toHtml(String text) { 
    return ("<html>" + text + "</html>"); 
} 

因此,例如,当我插入: "<resource>4</resource>"

输出为"4"。我想要的是输出为 "<resource>4</resource>"

我试着这样做:

return ("<html>" + text.replace("<","/<") + "</html>"); 
return ("<html>" + text.replace("<","\"<\"") + "</html>"); 

但标签仍然无法读取。

你能告诉我怎样才能逃避'<''>'字符?

+0

您正在从事一个非常高级的任务。您至少应该阅读HTML规范,包括[HTML文档表示 - 字符实体引用](https://www.w3.org/TR/html401/charset.html#h-5.3.2)。 – VGR

回答