本文目录导读:

JEditorPane、HTMLEditorKit 和 HTML 解析中的转义(Escape)操作,您可能遇到了以下几个常见场景或问题,以下是针对不同需求的详细解释和解决方案:
核心问题:HTML 内容中的特殊字符转义
当你在 JEditorPane 中显示 HTML 时,需要正确处理 <、>、& 等 HTML 实体。
自动转义(推荐)
JEditorPane 使用 HTMLEditorKit 时,不需要手动转义文本内容,因为它是 HTML 解析器,会直接解析 HTML 标签。
JEditorPane editor = new JEditorPane();
editor.setContentType("text/html");
editor.setText("<html><body><p>This is a <b>bold</b> text</p></body></html>");
手动转义文本(防止 XSS 或误解析)
如果你要在 HTML 中显示用户输入的内容,需要先转义特殊字符:
public static String escapeHTML(String input) {
if (input == null) return null;
StringBuilder out = new StringBuilder();
for (char c : input.toCharArray()) {
switch (c) {
case '<': out.append("<"); break;
case '>': out.append(">"); break;
case '&': out.append("&"); break;
case '"': out.append("""); break;
case '\'': out.append("'"); break;
default: out.append(c);
}
}
return out.toString();
}
// 使用示例
String userInput = "<script>alert('XSS')</script>";
String safeHTML = "<html><body>" + escapeHTML(userInput) + "</body></html>";
editor.setText(safeHTML);
逆向操作:从 HTML 中提取纯文本(Unescape)
如果你需要从 HTML 内容中提取纯文本(去除标签并还原实体):
import javax.swing.text.html.*;
import javax.swing.text.html.parser.*;
public static String htmlToPlainText(String html) {
try {
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
kit.read(new StringReader(html), doc, 0);
return doc.getText(0, doc.getLength());
} catch (Exception e) {
return html; // 失败时返回原始内容
}
}
// 使用
String html = "Hello & welcome to <Java>";
String plain = htmlToPlainText(html); // 返回: "Hello & welcome to <Java>"
常见问题排查
问题1:HTML 内容显示为纯文本
原因:没有设置正确的 contentType
解决:
editor.setContentType("text/html");
问题2:转义字符无效
原因:在 HTML 中,& 必须用 & 转义,否则解析器会尝试将其解释为实体开头
正确:
String text = "AT&T"; // 将显示为 "AT&T"
问题3:中文乱码
解决:确保使用 UTF-8 编码
editor.setContentType("text/html; charset=UTF-8");
// 或在 HTML 头部声明
String html = "<html><head><meta charset='UTF-8'></head><body>中文内容</body></html>";
高级用法:自定义实体解析
如果你需要处理自定义实体或覆盖默认行为,可以扩展 HTMLEditorKit.Parser:
public class CustomParser extends HTMLEditorKit.Parser {
@Override
public void parse(Reader r, HTMLEditorKit.ParserCallback cb, boolean ignoreCharSet) throws IOException {
// 自定义解析逻辑
super.parse(r, cb, ignoreCharSet);
}
}
// 在 HTMLEditorKit 中使用
HTMLEditorKit kit = new HTMLEditorKit() {
@Override
public HTMLEditorKit.Parser getParser() {
return new CustomParser();
}
};
完整示例:可编辑的 HTML 编辑器
import javax.swing.*;
import javax.swing.text.html.*;
public class HTMLEditorExample {
public static void main(String[] args) {
JFrame frame = new JFrame("HTML Editor");
JEditorPane editor = new JEditorPane();
editor.setContentType("text/html");
editor.setEditable(true);
// 初始化内容
String html = "<html><body>"
+ "<h1>Hello World</h1>"
+ "<p>This is <b>bold</b> and <i>italic</i></p>"
+ "<p>Special chars: < > & "</p>"
+ "</body></html>";
editor.setText(html);
JScrollPane scroll = new JScrollPane(editor);
frame.add(scroll);
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
| 操作 | 方法 |
|---|---|
| 显示 HTML | 直接设置 text/html 类型 |
| 转义用户输入 | escapeHTML() 自定义方法 |
| 提取纯文本 | 使用 HTMLDocument.getText() |
| 避免乱码 | 设置 UTF-8 编码 |
| 自定义解析 | 扩展 HTMLEditorKit.Parser |
如果你有具体的错误信息或特殊需求,请提供更多细节,我可以给出更精确的解决方案。
版权声明:足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播